summaryrefslogtreecommitdiffstats
path: root/tests/HwAccelerationTest/src/com/android/test/hwui/ViewLayerInvalidationActivity.java
blob: 6d47d6c467e78b93efaf09e536aaabef64ff6c96 (plain)
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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.test.hwui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class ViewLayerInvalidationActivity extends Activity {

    int currentColor = Color.WHITE;
    boolean nestedLayersOn = false;
    ArrayList<LinearLayout> linearLayouts = new ArrayList<LinearLayout>();
    ArrayList<LinearLayout> topLayouts = new ArrayList<LinearLayout>();
    ArrayList<TextView> textViews = new ArrayList<TextView>();
    LinearLayout container = null;
    boolean randomInvalidates = false;
    TextView nestedStatusTV, invalidateStatusTV;
    static final String NO_NESTING = "Nested Layer: NO   ";
    static final String NESTING = "Nested Layers: YES   ";
    static final String NO_INVALIDATING = "Random Invalidating: NO   ";
    static final String INVALIDATING = "Random Invalidating: YES   ";
    static final int TEXT_COLOR_INTERVAL = 400;
    static final int INVALIDATING_INTERVAL = 1000;
    static final int NESTING_INTERVAL = 2000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_layer_invalidation);

        container = (LinearLayout) findViewById(R.id.container);
        final LinearLayout container1 = (LinearLayout) findViewById(R.id.container1);
        final LinearLayout container2 = (LinearLayout) findViewById(R.id.container2);
        final LinearLayout container3 = (LinearLayout) findViewById(R.id.container3);
        nestedStatusTV = (TextView) findViewById(R.id.nestedStatus);
        invalidateStatusTV = (TextView) findViewById(R.id.invalidateStatus);
        final TextView tva = (TextView) findViewById(R.id.textviewa);

        topLayouts.add(container1);
        topLayouts.add(container2);
        topLayouts.add(container3);

        collectLinearLayouts(container);
        collectTextViews(container);

        nestedStatusTV.setText(NO_NESTING);
        invalidateStatusTV.setText(NO_INVALIDATING);

        tva.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        container1.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        container2.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        container3.setLayerType(View.LAYER_TYPE_HARDWARE, null);

        container.postDelayed(textColorSetter, TEXT_COLOR_INTERVAL);
        container.postDelayed(nestedLayerSetter, NESTING_INTERVAL);
        container.postDelayed(randomInvalidatesSetter, INVALIDATING_INTERVAL);
    }

    private Runnable textColorSetter = new Runnable() {
        @Override
        public void run() {
            currentColor = (currentColor == Color.WHITE) ? Color.RED : Color.WHITE;
            for (TextView tv : textViews) {
                tv.setTextColor(currentColor);
            }
            if (randomInvalidates) {
                randomInvalidator(container);
            }
            container.postDelayed(textColorSetter, TEXT_COLOR_INTERVAL);
        }
    };

    private Runnable randomInvalidatesSetter = new Runnable() {
        @Override
        public void run() {
            randomInvalidates = !randomInvalidates;
            invalidateStatusTV.setText(randomInvalidates ? INVALIDATING : NO_INVALIDATING);
            container.postDelayed(randomInvalidatesSetter, INVALIDATING_INTERVAL);
        }
    };

    private Runnable nestedLayerSetter = new Runnable() {
        @Override
        public void run() {
            nestedLayersOn = !nestedLayersOn;
            nestedStatusTV.setText(nestedLayersOn ? NESTING : NO_NESTING);
            for (LinearLayout layout : linearLayouts) {
                layout.setLayerType(nestedLayersOn ?
                        View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null);
            }
            if (!nestedLayersOn) {
                for (LinearLayout layout : topLayouts) {
                    layout.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                }
            }
            container.postDelayed(nestedLayerSetter, NESTING_INTERVAL);
        }
    };

    /**
     * Invalidates views based on random chance (50%). This is meant to test
     * invalidating several items in the hierarchy at the same time, which can cause artifacts
     * if our invalidation-propagation logic is not sound.
     */
    private void randomInvalidator(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); ++i) {
            View child = parent.getChildAt(i);
            if (Math.random() < .5) {
                child.invalidate();
            }
            if (child instanceof ViewGroup) {
                randomInvalidator((ViewGroup) child);
            }
        }
    }

    private void collectLinearLayouts(View view) {
        if (!(view instanceof LinearLayout)) {
            return;
        }
        LinearLayout parent = (LinearLayout) view;
        linearLayouts.add(parent);
        for (int i = 0; i < parent.getChildCount(); ++i) {
            collectLinearLayouts(parent.getChildAt(i));
        }
    }

    private void collectTextViews(View view) {
        if (view instanceof TextView) {
            textViews.add((TextView) view);
            return;
        }
        if (!(view instanceof ViewGroup)) {
            return;
        }
        ViewGroup parent = (ViewGroup) view;
        for (int i = 0; i < parent.getChildCount(); ++i) {
            collectTextViews(parent.getChildAt(i));
        }
    }
}