blob: 83f166844d13efba9b9b1d33bdff9f1134492d1b [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;
Chris Craik09a71e02011-12-12 18:03:29 -080036 private static final int LOAD_STALL_MILLIS = 2000; // nr of millis after load,
Chris Craik55ad2ef2011-08-19 17:35:20 -070037 // before test is forced
Chris Craik5888ec22011-07-15 16:24:37 -070038
39 public ProfiledWebView(Context context) {
40 super(context);
41 }
42
43 public ProfiledWebView(Context context, AttributeSet attrs) {
44 super(context, attrs);
45 }
46
47 public ProfiledWebView(Context context, AttributeSet attrs, int defStyle) {
48 super(context, attrs, defStyle);
49 }
50
51 public ProfiledWebView(Context context, AttributeSet attrs, int defStyle,
52 boolean privateBrowsing) {
53 super(context, attrs, defStyle, privateBrowsing);
54 }
55
56 @Override
57 protected void onDraw(android.graphics.Canvas canvas) {
Chris Craik55ad2ef2011-08-19 17:35:20 -070058 if (mIsTesting && mIsScrolling) {
Chris Craik5888ec22011-07-15 16:24:37 -070059 if (canScrollVertically(1)) {
60 scrollBy(0, mSpeed);
61 } else {
62 stopScrollTest();
Chris Craik55ad2ef2011-08-19 17:35:20 -070063 mIsScrolling = false;
Chris Craik5888ec22011-07-15 16:24:37 -070064 }
65 }
66 super.onDraw(canvas);
67 }
68
69 /*
Chris Craik21555ab2011-07-21 11:52:19 -070070 * Called once the page is loaded to start scrolling for evaluating tiles.
Chris Craik555c55e2011-07-28 15:39:43 -070071 * If autoScrolling isn't set, stop must be called manually. Before
72 * scrolling, invalidate all content and redraw it, measuring time taken.
Chris Craik5888ec22011-07-15 16:24:37 -070073 */
Chris Craik21555ab2011-07-21 11:52:19 -070074 public void startScrollTest(ProfileCallback callback, boolean autoScrolling) {
Chris Craik55ad2ef2011-08-19 17:35:20 -070075 mIsScrolling = autoScrolling;
Chris Craik5888ec22011-07-15 16:24:37 -070076 mCallback = callback;
Chris Craik55ad2ef2011-08-19 17:35:20 -070077 mIsTesting = false;
Chris Craik55ad2ef2011-08-19 17:35:20 -070078
79 if (autoScrolling) {
80 // after a while, force it to start even if the pages haven't swapped
81 new CountDownTimer(LOAD_STALL_MILLIS, LOAD_STALL_MILLIS) {
82 @Override
83 public void onTick(long millisUntilFinished) {
84 }
85
86 @Override
87 public void onFinish() {
Chris Craik09a71e02011-12-12 18:03:29 -080088 // invalidate all content, and kick off redraw
89 registerPageSwapCallback();
90 discardAllTextures();
91 invalidate();
92
93 mContentInvalMillis = System.currentTimeMillis();
Chris Craik55ad2ef2011-08-19 17:35:20 -070094 }
95 }.start();
96 }
Chris Craik5888ec22011-07-15 16:24:37 -070097 }
98
99 /*
Chris Craik555c55e2011-07-28 15:39:43 -0700100 * Called after the manual contentInvalidateAll, after the tiles have all
101 * been redrawn.
102 */
103 @Override
Chris Craikd0051c02011-11-29 10:26:10 -0800104 protected void pageSwapCallback(boolean startAnim) {
Chris Craik555c55e2011-07-28 15:39:43 -0700105 mContentInvalMillis = System.currentTimeMillis() - mContentInvalMillis;
Chris Craikd0051c02011-11-29 10:26:10 -0800106 super.pageSwapCallback(startAnim);
Chris Craik555c55e2011-07-28 15:39:43 -0700107 Log.d("ProfiledWebView", "REDRAW TOOK " + mContentInvalMillis
108 + "millis");
Chris Craik55ad2ef2011-08-19 17:35:20 -0700109 mIsTesting = true;
Chris Craik555c55e2011-07-28 15:39:43 -0700110 invalidate(); // ensure a redraw so that auto-scrolling can occur
111 tileProfilingStart();
112 }
113
114 /*
Chris Craik5888ec22011-07-15 16:24:37 -0700115 * Called once the page has stopped scrolling
116 */
117 public void stopScrollTest() {
Chris Craik555c55e2011-07-28 15:39:43 -0700118 tileProfilingStop();
Chris Craik55ad2ef2011-08-19 17:35:20 -0700119 mIsTesting = false;
Chris Craik21555ab2011-07-21 11:52:19 -0700120
121 if (mCallback == null) {
122 tileProfilingClear();
123 return;
124 }
Chris Craik5888ec22011-07-15 16:24:37 -0700125
Chris Craik555c55e2011-07-28 15:39:43 -0700126 RunData data = new RunData(super.tileProfilingNumFrames());
Chris Craik55ad2ef2011-08-19 17:35:20 -0700127 // record the time spent (before scrolling) rendering the page
Chris Craik555c55e2011-07-28 15:39:43 -0700128 data.singleStats.put(getResources().getString(R.string.render_millis),
129 (double)mContentInvalMillis);
Chris Craik55ad2ef2011-08-19 17:35:20 -0700130 // record if the page render timed out
131 Log.d("ProfiledWebView", "hadtobeforced = " + mHadToBeForced);
132 data.singleStats.put(getResources().getString(R.string.render_stalls),
133 mHadToBeForced ? 1.0 : 0.0);
134 mHadToBeForced = false;
135
Chris Craik555c55e2011-07-28 15:39:43 -0700136 for (int frame = 0; frame < data.frames.length; frame++) {
137 data.frames[frame] = new TileData[
Chris Craik21555ab2011-07-21 11:52:19 -0700138 tileProfilingNumTilesInFrame(frame)];
Chris Craik555c55e2011-07-28 15:39:43 -0700139 for (int tile = 0; tile < data.frames[frame].length; tile++) {
Chris Craik21555ab2011-07-21 11:52:19 -0700140 int left = tileProfilingGetInt(frame, tile, "left");
141 int top = tileProfilingGetInt(frame, tile, "top");
142 int right = tileProfilingGetInt(frame, tile, "right");
143 int bottom = tileProfilingGetInt(frame, tile, "bottom");
Chris Craik5888ec22011-07-15 16:24:37 -0700144
Chris Craik21555ab2011-07-21 11:52:19 -0700145 boolean isReady = super.tileProfilingGetInt(
146 frame, tile, "isReady") == 1;
147 int level = tileProfilingGetInt(frame, tile, "level");
148
149 float scale = tileProfilingGetFloat(frame, tile, "scale");
150
Chris Craik555c55e2011-07-28 15:39:43 -0700151 data.frames[frame][tile] = data.new TileData(left, top, right, bottom,
Chris Craik21555ab2011-07-21 11:52:19 -0700152 isReady, level, scale);
Chris Craik5888ec22011-07-15 16:24:37 -0700153 }
154 }
Chris Craik555c55e2011-07-28 15:39:43 -0700155 tileProfilingClear();
Chris Craik5888ec22011-07-15 16:24:37 -0700156
157 mCallback.profileCallback(data);
158 }
159
160 @Override
161 public void loadUrl(String url) {
Chris Craik555c55e2011-07-28 15:39:43 -0700162 if (!url.startsWith("http://") && !url.startsWith("file://")) {
Chris Craik5888ec22011-07-15 16:24:37 -0700163 url = "http://" + url;
164 }
165 super.loadUrl(url);
166 }
167
168 public void setAutoScrollSpeed(int speedInt) {
169 mSpeed = speedInt;
170 }
171}