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