1/*
2 * Copyright (C) 2012 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.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.Path;
24import android.os.Bundle;
25import android.util.MathUtils;
26import android.view.View;
27
28import java.util.Random;
29
30/**
31 * The point of this test is to ensure that we can cause many paths to be created, drawn,
32 * and destroyed without causing hangs or crashes. This tests the native reference counting
33 * scheme in particular, because we should be able to have the Java-level path finalized
34 * without destroying the underlying native path object until we are done referencing it
35 * in pending DisplayLists.
36 */
37public class PathDestructionActivity extends Activity {
38
39    private static final int MIN_SIZE = 20;
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43
44        MyView view = new MyView(this);
45        setContentView(view);
46    }
47
48    private static class MyView extends View {
49        Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
50        Paint fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
51        Paint fillAndStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
52
53        private MyView(Context context) {
54            super(context);
55            strokePaint.setStyle(Paint.Style.STROKE);
56            fillPaint.setStyle(Paint.Style.FILL);
57            fillAndStrokePaint.setStyle(Paint.Style.FILL_AND_STROKE);
58        }
59
60        private Path getRandomPath() {
61            float left, top, right, bottom;
62            Random r = new Random();
63            left = r.nextFloat() * (getWidth() - MIN_SIZE);
64            top = r.nextFloat() * (getHeight() - MIN_SIZE);
65            right = left + r.nextFloat() * (getWidth() - left);
66            bottom = top + r.nextFloat() * (getHeight() - top);
67            Path path = new Path();
68            path.moveTo(left, top);
69            path.lineTo(right, top);
70            path.lineTo(right, bottom);
71            path.lineTo(left, bottom);
72            path.close();
73            return path;
74        }
75
76        private int getRandomColor() {
77            Random r = new Random();
78            int red = r.nextInt(255);
79            int green = r.nextInt(255);
80            int blue = r.nextInt(255);
81            return 0xff000000 | red << 16 | green << 8 | blue;
82        }
83
84        @Override
85        protected void onDraw(Canvas canvas) {
86            Path path;
87            for (int i = 0; i < 15; ++i) {
88                path = getRandomPath();
89                strokePaint.setColor(getRandomColor());
90                canvas.drawPath(path, strokePaint);
91                path = null;
92                path = getRandomPath();
93                fillPaint.setColor(getRandomColor());
94                canvas.drawPath(path, fillPaint);
95                path = null;
96                path = getRandomPath();
97                fillAndStrokePaint.setColor(getRandomColor());
98                canvas.drawPath(path, fillAndStrokePaint);
99                path = null;
100            }
101
102            invalidate();
103        }
104    }
105}
106