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 import 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 */ 37 public class PathDestructionActivity extends Activity { 38 39 private static final int MIN_SIZE = 20; 40 @Override onCreate(Bundle savedInstanceState)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 MyView(Context context)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 getRandomPath()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 getRandomColor()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 onDraw(Canvas canvas)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