1/* 2 * Copyright (C) 2007 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 android.graphics; 18 19import junit.framework.Assert; 20import android.content.Context; 21import android.content.res.Resources; 22import android.graphics.Bitmap; 23import android.graphics.BitmapFactory; 24import android.graphics.Canvas; 25import android.graphics.Paint; 26import android.test.AndroidTestCase; 27import android.test.PerformanceTestCase; 28import android.test.suitebuilder.annotation.Suppress; 29import android.util.Log; 30 31import com.android.frameworks.graphicstests.R; 32 33/** 34 * Graphics Performance Tests 35 * 36 */ 37//We don't want to run these perf tests in the continuous build. 38@Suppress 39public class GraphicsPerformanceTests { 40 private static final String TAG = "GfxPerf"; 41 public static String[] children() { 42 return new String[] { 43 // test decoding bitmaps of various sizes 44 DecodeBitmapTest.class.getName(), 45 46 // odd-sized bitmap drawing tests 47 DrawBitmap7x7.class.getName(), 48 DrawBitmap15x15.class.getName(), 49 DrawBitmap31x31.class.getName(), 50 DrawBitmap63x63.class.getName(), 51 DrawBitmap127x127.class.getName(), 52 DrawBitmap319x239.class.getName(), 53 DrawBitmap319x479.class.getName(), 54 55 // even-sized bitmap drawing tests 56 DrawBitmap8x8.class.getName(), 57 DrawBitmap16x16.class.getName(), 58 DrawBitmap32x32.class.getName(), 59 DrawBitmap64x64.class.getName(), 60 DrawBitmap128x128.class.getName(), 61 DrawBitmap320x240.class.getName(), 62 DrawBitmap320x480.class.getName()}; 63 } 64 65 /** 66 * Base class for all graphics tests 67 * 68 */ 69 public static abstract class GraphicsTestBase extends AndroidTestCase 70 implements PerformanceTestCase { 71 /** Target "screen" (bitmap) width and height */ 72 private static final int DEFAULT_ITERATIONS = 1; 73 private static final int SCREEN_WIDTH = 320; 74 private static final int SCREEN_HEIGHT = 480; 75 76 /** Number of iterations to pass back to harness. Subclass should override */ 77 protected int mIterations = 1; 78 79 /** Bitmap we allocate and draw to */ 80 protected Bitmap mDestBitmap; 81 82 /** Canvas of drawing routines */ 83 protected Canvas mCanvas; 84 85 /** Style and color information (uses defaults) */ 86 protected Paint mPaint; 87 88 @Override 89 public void setUp() throws Exception { 90 super.setUp(); 91 // Create drawable bitmap for rendering into 92 mDestBitmap = Bitmap.createBitmap(SCREEN_WIDTH, SCREEN_HEIGHT, 93 Bitmap.Config.RGB_565); 94 // Set of drawing routines 95 mCanvas = new Canvas(mDestBitmap); 96 // Styles 97 mPaint = new Paint(); 98 // Ask subclass for number of iterations 99 mIterations = getIterations(); 100 } 101 102 // A reasonable default 103 public int getIterations() { 104 return DEFAULT_ITERATIONS; 105 } 106 107 public boolean isPerformanceOnly() { 108 return true; 109 } 110 111 public int startPerformance(Intermediates intermediates) { 112 intermediates.setInternalIterations(mIterations * 10); 113 return 0; 114 } 115 } 116 117 /** 118 * Tests time to decode a number of sizes of images. 119 */ 120 public static class DecodeBitmapTest extends GraphicsTestBase { 121 /** Number of times to run this test */ 122 private static final int DECODE_ITERATIONS = 10; 123 124 /** Used to access package bitmap images */ 125 private Resources mResources; 126 127 @Override 128 public void setUp() throws Exception { 129 super.setUp(); 130 131 // For bitmap resources 132 Context context = getContext(); 133 Assert.assertNotNull(context); 134 mResources = context.getResources(); 135 Assert.assertNotNull(mResources); 136 } 137 138 @Override 139 public int getIterations() { 140 return DECODE_ITERATIONS; 141 } 142 143 public void testDecodeBitmap() { 144 for (int i = 0; i < DECODE_ITERATIONS; i++) { 145 BitmapFactory.decodeResource(mResources, R.drawable.test16x12); 146 BitmapFactory.decodeResource(mResources, R.drawable.test32x24); 147 BitmapFactory.decodeResource(mResources, R.drawable.test64x48); 148 BitmapFactory.decodeResource(mResources, R.drawable.test128x96); 149 BitmapFactory.decodeResource(mResources, R.drawable.test256x192); 150 BitmapFactory.decodeResource(mResources, R.drawable.test320x240); 151 } 152 } 153 } 154 155 /** 156 * Base class for bitmap drawing tests 157 * 158 */ 159 public static abstract class DrawBitmapTest extends GraphicsTestBase { 160 /** Number of times to run each draw test */ 161 private static final int ITERATIONS = 1000; 162 163 /** Bitmap to draw. Allocated by subclass's createBitmap() function. */ 164 private Bitmap mBitmap; 165 166 @Override 167 public void setUp() throws Exception { 168 super.setUp(); 169 170 // Invoke subclass's method to create the bitmap 171 mBitmap = createBitmap(); 172 } 173 174 public int getIterations() { 175 return ITERATIONS; 176 } 177 178 // Generic abstract function to create bitmap for any given subclass 179 public abstract Bitmap createBitmap(); 180 181 // Provide convenience test code for all subsequent classes. 182 // Note: Though it would be convenient to declare all of the test*() methods here 183 // and just inherit them, our test harness doesn't support it. So we replicate 184 // a bit of code in each derived test case. 185 public void drawBitmapEven() { 186 for (int i = 0; i < ITERATIONS; i++) { 187 mCanvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); 188 } 189 } 190 191 public void drawBitmapOdd() { 192 for (int i = 0; i < ITERATIONS; i++) { 193 mCanvas.drawBitmap(mBitmap, 1.0f, 0.0f, mPaint); 194 } 195 } 196 } 197 198 199 /** 200 * Test drawing of 7x7 image 201 */ 202 public static class DrawBitmap7x7 extends DrawBitmapTest { 203 204 public Bitmap createBitmap() { 205 return Bitmap.createBitmap(7, 7, Bitmap.Config.RGB_565); 206 } 207 208 public void testDrawBitmapEven() { 209 drawBitmapEven(); 210 } 211 212 public void testDrawBitmapOdd() { 213 drawBitmapOdd(); 214 } 215 } 216 217 /** 218 * Test drawing of 15x15 image 219 */ 220 public static class DrawBitmap15x15 extends DrawBitmapTest { 221 222 public Bitmap createBitmap() { 223 return Bitmap.createBitmap(15, 15, Bitmap.Config.RGB_565); 224 } 225 226 public void testDrawBitmapEven() { 227 drawBitmapEven(); 228 } 229 230 public void testDrawBitmapOdd() { 231 drawBitmapOdd(); 232 } 233 } 234 235 /** 236 * Test drawing of 31x31 image 237 */ 238 public static class DrawBitmap31x31 extends DrawBitmapTest { 239 240 public Bitmap createBitmap() { 241 return Bitmap.createBitmap(31, 31, Bitmap.Config.RGB_565); 242 } 243 244 public void testDrawBitmapEven() { 245 drawBitmapEven(); 246 } 247 248 public void testDrawBitmapOdd() { 249 drawBitmapOdd(); 250 } 251 } 252 253 /** 254 * Test drawing of 63x63 image 255 */ 256 public static class DrawBitmap63x63 extends DrawBitmapTest { 257 258 public Bitmap createBitmap() { 259 return Bitmap.createBitmap(63, 63, Bitmap.Config.RGB_565); 260 } 261 262 public void testDrawBitmapEven() { 263 drawBitmapEven(); 264 } 265 266 public void testDrawBitmapOdd() { 267 drawBitmapOdd(); 268 } 269 } 270 271 /** 272 * Test drawing of 127x127 image 273 */ 274 public static class DrawBitmap127x127 extends DrawBitmapTest { 275 276 public Bitmap createBitmap() { 277 return Bitmap.createBitmap(127, 127, Bitmap.Config.RGB_565); 278 } 279 280 public void testDrawBitmapEven() { 281 drawBitmapEven(); 282 } 283 284 public void testDrawBitmapOdd() { 285 drawBitmapOdd(); 286 } 287 } 288 289 /** 290 * Test drawing of 319x239 image 291 */ 292 public static class DrawBitmap319x239 extends DrawBitmapTest { 293 294 public Bitmap createBitmap() { 295 return Bitmap.createBitmap(319, 239, Bitmap.Config.RGB_565); 296 } 297 298 public void testDrawBitmapEven() { 299 drawBitmapEven(); 300 } 301 302 public void testDrawBitmapOdd() { 303 drawBitmapOdd(); 304 } 305 } 306 307 /** 308 * Test drawing of 319x479 image 309 */ 310 public static class DrawBitmap319x479 extends DrawBitmapTest { 311 312 public Bitmap createBitmap() { 313 return Bitmap.createBitmap(319, 479, Bitmap.Config.RGB_565); 314 } 315 316 public void testDrawBitmapEven() { 317 drawBitmapEven(); 318 } 319 320 public void testDrawBitmapOdd() { 321 drawBitmapOdd(); 322 } 323 } 324 325 /** 326 * Test drawing of 8x8 image 327 */ 328 public static class DrawBitmap8x8 extends DrawBitmapTest { 329 330 public Bitmap createBitmap() { 331 return Bitmap.createBitmap(8, 8, Bitmap.Config.RGB_565); 332 } 333 334 public void testDrawBitmapEven() { 335 drawBitmapEven(); 336 } 337 338 public void testDrawBitmapOdd() { 339 drawBitmapOdd(); 340 } 341 } 342 343 /** 344 * Test drawing of 16x16 image 345 */ 346 public static class DrawBitmap16x16 extends DrawBitmapTest { 347 348 public Bitmap createBitmap() { 349 return Bitmap.createBitmap(16, 16, Bitmap.Config.RGB_565); 350 } 351 352 public void testDrawBitmapEven() { 353 drawBitmapEven(); 354 } 355 356 public void testDrawBitmapOdd() { 357 drawBitmapOdd(); 358 } 359 } 360 361 /** 362 * Test drawing of 32x32 image 363 */ 364 public static class DrawBitmap32x32 extends DrawBitmapTest { 365 366 public Bitmap createBitmap() { 367 return Bitmap.createBitmap(32, 32, Bitmap.Config.RGB_565); 368 } 369 370 public void testDrawBitmapEven() { 371 drawBitmapEven(); 372 } 373 374 public void testDrawBitmapOdd() { 375 drawBitmapOdd(); 376 } 377 } 378 379 /** 380 * Test drawing of 64x64 image 381 */ 382 public static class DrawBitmap64x64 extends DrawBitmapTest { 383 384 public Bitmap createBitmap() { 385 return Bitmap.createBitmap(64, 64, Bitmap.Config.RGB_565); 386 } 387 388 public void testDrawBitmapEven() { 389 drawBitmapEven(); 390 } 391 392 public void testDrawBitmapOdd() { 393 drawBitmapOdd(); 394 } 395 } 396 397 /** 398 * Test drawing of 128x128 image 399 */ 400 public static class DrawBitmap128x128 extends DrawBitmapTest { 401 402 public Bitmap createBitmap() { 403 return Bitmap.createBitmap(128, 128, Bitmap.Config.RGB_565); 404 } 405 406 public void testDrawBitmapEven() { 407 drawBitmapEven(); 408 } 409 410 public void testDrawBitmapOdd() { 411 drawBitmapOdd(); 412 } 413 } 414 415 /** 416 * Test drawing of 320x240 image 417 */ 418 public static class DrawBitmap320x240 extends DrawBitmapTest { 419 420 public Bitmap createBitmap() { 421 return Bitmap.createBitmap(320, 240, Bitmap.Config.RGB_565); 422 } 423 424 public void testDrawBitmapEven() { 425 drawBitmapEven(); 426 } 427 428 public void testDrawBitmapOdd() { 429 drawBitmapOdd(); 430 } 431 } 432 433 /** 434 * Test drawing of 320x480 image 435 */ 436 public static class DrawBitmap320x480 extends DrawBitmapTest { 437 438 public Bitmap createBitmap() { 439 return Bitmap.createBitmap(320, 480, Bitmap.Config.RGB_565); 440 } 441 442 public void testDrawBitmapEven() { 443 drawBitmapEven(); 444 } 445 446 public void testDrawBitmapOdd() { 447 drawBitmapOdd(); 448 } 449 } 450} 451