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