| Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package test.windowmanagerstresstest; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.graphics.Rect; |
| 21 | import android.os.Bundle; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.SystemClock; |
| 24 | import android.util.Log; |
| 25 | import android.util.MergedConfiguration; |
| 26 | import android.view.Display; |
| 27 | import android.view.IWindowSession; |
| 28 | import android.view.Surface; |
| 29 | import android.view.View; |
| 30 | import android.view.WindowManager; |
| 31 | import android.view.WindowManager.LayoutParams; |
| 32 | import android.view.WindowManagerGlobal; |
| 33 | import android.widget.TextView; |
| 34 | |
| 35 | import com.android.internal.view.BaseIWindow; |
| 36 | |
| 37 | import java.util.ArrayList; |
| 38 | |
| 39 | public class MainActivity extends Activity { |
| 40 | |
| 41 | private static final String TAG = "WmSlam"; |
| 42 | |
| 43 | private TextView mOutput; |
| 44 | private volatile boolean finished; |
| 45 | private final ArrayList<BaseIWindow> mWindows = new ArrayList<>(); |
| 46 | private final LayoutParams mLayoutParams = new LayoutParams(); |
| 47 | private final Rect mTmpRect = new Rect(); |
| 48 | |
| 49 | @Override |
| 50 | protected void onCreate(Bundle savedInstanceState) { |
| 51 | super.onCreate(savedInstanceState); |
| 52 | setContentView(R.layout.activity_main); |
| 53 | mOutput = (TextView) findViewById(R.id.output); |
| 54 | |
| 55 | findViewById(R.id.run).setOnClickListener(view -> { |
| 56 | view.setEnabled(false); |
| 57 | mOutput.setText(""); |
| 58 | startBatch(); |
| 59 | }); |
| 60 | mLayoutParams.token = getActivityToken(); |
| 61 | } |
| 62 | |
| 63 | void startBatch() { |
| 64 | new Thread(() -> { |
| 65 | finished = false; |
| 66 | addWindows(); |
| 67 | startCpuRunnables(); |
| 68 | for (int i = 0; i < 5; i++) { |
| 69 | final long time = SystemClock.uptimeMillis(); |
| 70 | slamWm(); |
| 71 | log("Total: " + (SystemClock.uptimeMillis() - time) + " ms"); |
| 72 | } |
| 73 | removeWindows(); |
| 74 | finished = true; |
| 75 | }).start(); |
| 76 | } |
| 77 | |
| 78 | void startCpuRunnables() { |
| 79 | for (int i = 0; i < 10; i++) { |
| 80 | new Thread(mUseCpuRunnable).start(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private final Runnable mUseCpuRunnable = new Runnable() { |
| 85 | @Override |
| 86 | public void run() { |
| 87 | while (!finished) { |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | private void log(String text) { |
| 93 | mOutput.post(() -> mOutput.append(text + "\n")); |
| 94 | Log.d(TAG, text); |
| 95 | } |
| 96 | |
| 97 | private void slamWm() { |
| 98 | ArrayList<Thread> threads = new ArrayList<>(); |
| 99 | for (int i = 0; i < 20; i++) { |
| 100 | for (BaseIWindow window : mWindows) { |
| 101 | Thread t = new Thread(() -> { |
| 102 | try { |
| 103 | WindowManagerGlobal.getWindowSession().relayout(window, |
| 104 | window.mSeq, mLayoutParams, -1, -1, View.VISIBLE, 0, mTmpRect, |
| 105 | mTmpRect, mTmpRect, mTmpRect, mTmpRect, mTmpRect, mTmpRect, |
| 106 | new MergedConfiguration(), new Surface()); |
| 107 | } catch (RemoteException e) { |
| 108 | e.printStackTrace(); |
| 109 | } |
| 110 | }); |
| 111 | threads.add(t); |
| 112 | t.start(); |
| 113 | } |
| 114 | } |
| 115 | for (Thread t : threads) { |
| 116 | try { |
| 117 | t.join(); |
| 118 | } catch (InterruptedException e) { |
| 119 | e.printStackTrace(); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void addWindows() { |
| 125 | for (int i = 0; i < 50; i++) { |
| 126 | final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); |
| 127 | layoutParams.token = getActivityToken(); |
| 128 | final BaseIWindow window = new BaseIWindow(); |
| 129 | final IWindowSession session = WindowManagerGlobal.getWindowSession(); |
| 130 | final Rect tmpRect = new Rect(); |
| 131 | try { |
| 132 | final int res = session.addToDisplayWithoutInputChannel(window, window.mSeq, layoutParams, |
| 133 | View.VISIBLE, Display.DEFAULT_DISPLAY, tmpRect, tmpRect); |
| 134 | } catch (RemoteException e) { |
| 135 | e.printStackTrace(); |
| 136 | } |
| 137 | mWindows.add(window); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void removeWindows() { |
| 142 | for (BaseIWindow window : mWindows) { |
| 143 | try { |
| 144 | WindowManagerGlobal.getWindowSession().remove(window); |
| 145 | } catch (RemoteException e) { |
| 146 | e.printStackTrace(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |