1 /* 2 * Copyright (C) 2010 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 18 /* 19 * Hardware Composer stress test 20 * 21 * Performs a pseudo-random (prandom) sequence of operations to the 22 * Hardware Composer (HWC), for a specified number of passes or for 23 * a specified period of time. By default the period of time is FLT_MAX, 24 * so that the number of passes will take precedence. 25 * 26 * The passes are grouped together, where (pass / passesPerGroup) specifies 27 * which group a particular pass is in. This causes every passesPerGroup 28 * worth of sequential passes to be within the same group. Computationally 29 * intensive operations are performed just once at the beginning of a group 30 * of passes and then used by all the passes in that group. This is done 31 * so as to increase both the average and peak rate of graphic operations, 32 * by moving computationally intensive operations to the beginning of a group. 33 * In particular, at the start of each group of passes a set of 34 * graphic buffers are created, then used by the first and remaining 35 * passes of that group of passes. 36 * 37 * The per-group initialization of the graphic buffers is performed 38 * by a function called initFrames. This function creates an array 39 * of smart pointers to the graphic buffers, in the form of a vector 40 * of vectors. The array is accessed in row major order, so each 41 * row is a vector of smart pointers. All the pointers of a single 42 * row point to graphic buffers which use the same pixel format and 43 * have the same dimension, although it is likely that each one is 44 * filled with a different color. This is done so that after doing 45 * the first HWC prepare then set call, subsequent set calls can 46 * be made with each of the layer handles changed to a different 47 * graphic buffer within the same row. Since the graphic buffers 48 * in a particular row have the same pixel format and dimension, 49 * additional HWC set calls can be made, without having to perform 50 * an HWC prepare call. 51 * 52 * This test supports the following command-line options: 53 * 54 * -v Verbose 55 * -s num Starting pass 56 * -e num Ending pass 57 * -p num Execute the single pass specified by num 58 * -n num Number of set operations to perform after each prepare operation 59 * -t float Maximum time in seconds to execute the test 60 * -d float Delay in seconds performed after each set operation 61 * -D float Delay in seconds performed after the last pass is executed 62 * 63 * Typically the test is executed for a large range of passes. By default 64 * passes 0 through 99999 (100,000 passes) are executed. Although this test 65 * does not validate the generated image, at times it is useful to reexecute 66 * a particular pass and leave the displayed image on the screen for an 67 * extended period of time. This can be done either by setting the -s 68 * and -e options to the desired pass, along with a large value for -D. 69 * This can also be done via the -p option, again with a large value for 70 * the -D options. 71 * 72 * So far this test only contains code to create graphic buffers with 73 * a continuous solid color. Although this test is unable to validate the 74 * image produced, any image that contains other than rectangles of a solid 75 * color are incorrect. Note that the rectangles may use a transparent 76 * color and have a blending operation that causes the color in overlapping 77 * rectangles to be mixed. In such cases the overlapping portions may have 78 * a different color from the rest of the rectangle. 79 */ 80 81 #include <algorithm> 82 #include <assert.h> 83 #include <cerrno> 84 #include <cmath> 85 #include <cstdlib> 86 #include <ctime> 87 #include <libgen.h> 88 #include <sched.h> 89 #include <sstream> 90 #include <stdint.h> 91 #include <string.h> 92 #include <unistd.h> 93 #include <vector> 94 95 #include <sys/syscall.h> 96 #include <sys/types.h> 97 #include <sys/wait.h> 98 99 #include <EGL/egl.h> 100 #include <EGL/eglext.h> 101 #include <GLES2/gl2.h> 102 #include <GLES2/gl2ext.h> 103 104 #include <ui/FramebufferNativeWindow.h> 105 #include <ui/GraphicBuffer.h> 106 107 #define LOG_TAG "hwcStressTest" 108 #include <utils/Log.h> 109 #include <testUtil.h> 110 111 #include <hardware/hwcomposer.h> 112 113 #include <glTestLib.h> 114 #include "hwcTestLib.h" 115 116 using namespace std; 117 using namespace android; 118 119 const float maxSizeRatio = 1.3; // Graphic buffers can be upto this munch 120 // larger than the default screen size 121 const unsigned int passesPerGroup = 10; // A group of passes all use the same 122 // graphic buffers 123 124 // Ratios at which rare and frequent conditions should be produced 125 const float rareRatio = 0.1; 126 const float freqRatio = 0.9; 127 128 // Defaults for command-line options 129 const bool defaultVerbose = false; 130 const unsigned int defaultStartPass = 0; 131 const unsigned int defaultEndPass = 99999; 132 const unsigned int defaultPerPassNumSet = 10; 133 const float defaultPerSetDelay = 0.0; // Default delay after each set 134 // operation. Default delay of 135 // zero used so as to perform the 136 // the set operations as quickly 137 // as possible. 138 const float defaultEndDelay = 2.0; // Default delay between completion of 139 // final pass and restart of framework 140 const float defaultDuration = FLT_MAX; // A fairly long time, so that 141 // range of passes will have 142 // precedence 143 144 // Command-line option settings 145 static bool verbose = defaultVerbose; 146 static unsigned int startPass = defaultStartPass; 147 static unsigned int endPass = defaultEndPass; 148 static unsigned int numSet = defaultPerPassNumSet; 149 static float perSetDelay = defaultPerSetDelay; 150 static float endDelay = defaultEndDelay; 151 static float duration = defaultDuration; 152 153 // Command-line mutual exclusion detection flags. 154 // Corresponding flag set true once an option is used. 155 bool eFlag, sFlag, pFlag; 156 157 #define MAXSTR 100 158 #define MAXCMD 200 159 #define BITSPERBYTE 8 // TODO: Obtain from <values.h>, once 160 // it has been added 161 162 #define CMD_STOP_FRAMEWORK "stop 2>&1" 163 #define CMD_START_FRAMEWORK "start 2>&1" 164 165 #define NUMA(a) (sizeof(a) / sizeof(a [0])) 166 #define MEMCLR(addr, size) do { \ 167 memset((addr), 0, (size)); \ 168 } while (0) 169 170 // File scope constants 171 const unsigned int blendingOps[] = { 172 HWC_BLENDING_NONE, 173 HWC_BLENDING_PREMULT, 174 HWC_BLENDING_COVERAGE, 175 }; 176 const unsigned int layerFlags[] = { 177 HWC_SKIP_LAYER, 178 }; 179 const vector<unsigned int> vecLayerFlags(layerFlags, 180 layerFlags + NUMA(layerFlags)); 181 182 const unsigned int transformFlags[] = { 183 HWC_TRANSFORM_FLIP_H, 184 HWC_TRANSFORM_FLIP_V, 185 HWC_TRANSFORM_ROT_90, 186 // ROT_180 & ROT_270 intentionally not listed, because they 187 // they are formed from combinations of the flags already listed. 188 }; 189 const vector<unsigned int> vecTransformFlags(transformFlags, 190 transformFlags + NUMA(transformFlags)); 191 192 // File scope globals 193 static const int texUsage = GraphicBuffer::USAGE_HW_TEXTURE | 194 GraphicBuffer::USAGE_SW_WRITE_RARELY; 195 static hwc_composer_device_t *hwcDevice; 196 static EGLDisplay dpy; 197 static EGLSurface surface; 198 static EGLint width, height; 199 static vector <vector <sp<GraphicBuffer> > > frames; 200 201 // File scope prototypes 202 void init(void); 203 void initFrames(unsigned int seed); 204 template <class T> vector<T> vectorRandSelect(const vector<T>& vec, size_t num); 205 template <class T> T vectorOr(const vector<T>& vec); 206 207 /* 208 * Main 209 * 210 * Performs the following high-level sequence of operations: 211 * 212 * 1. Command-line parsing 213 * 214 * 2. Initialization 215 * 216 * 3. For each pass: 217 * 218 * a. If pass is first pass or in a different group from the 219 * previous pass, initialize the array of graphic buffers. 220 * 221 * b. Create a HWC list with room to specify a prandomly 222 * selected number of layers. 223 * 224 * c. Select a subset of the rows from the graphic buffer array, 225 * such that there is a unique row to be used for each 226 * of the layers in the HWC list. 227 * 228 * d. Prandomly fill in the HWC list with handles 229 * selected from any of the columns of the selected row. 230 * 231 * e. Pass the populated list to the HWC prepare call. 232 * 233 * f. Pass the populated list to the HWC set call. 234 * 235 * g. If additional set calls are to be made, then for each 236 * additional set call, select a new set of handles and 237 * perform the set call. 238 */ 239 int 240 main(int argc, char *argv[]) 241 { 242 int rv, opt; 243 char *chptr; 244 unsigned int pass; 245 char cmd[MAXCMD]; 246 struct timeval startTime, currentTime, delta; 247 248 testSetLogCatTag(LOG_TAG); 249 250 // Parse command line arguments 251 while ((opt = getopt(argc, argv, "vp:d:D:n:s:e:t:?h")) != -1) { 252 switch (opt) { 253 case 'd': // Delay after each set operation 254 perSetDelay = strtod(optarg, &chptr); 255 if ((*chptr != '\0') || (perSetDelay < 0.0)) { 256 testPrintE("Invalid command-line specified per pass delay of: " 257 "%s", optarg); 258 exit(1); 259 } 260 break; 261 262 case 'D': // End of test delay 263 // Delay between completion of final pass and restart 264 // of framework 265 endDelay = strtod(optarg, &chptr); 266 if ((*chptr != '\0') || (endDelay < 0.0)) { 267 testPrintE("Invalid command-line specified end of test delay " 268 "of: %s", optarg); 269 exit(2); 270 } 271 break; 272 273 case 't': // Duration 274 duration = strtod(optarg, &chptr); 275 if ((*chptr != '\0') || (duration < 0.0)) { 276 testPrintE("Invalid command-line specified duration of: %s", 277 optarg); 278 exit(3); 279 } 280 break; 281 282 case 'n': // Num set operations per pass 283 numSet = strtoul(optarg, &chptr, 10); 284 if (*chptr != '\0') { 285 testPrintE("Invalid command-line specified num set per pass " 286 "of: %s", optarg); 287 exit(4); 288 } 289 break; 290 291 case 's': // Starting Pass 292 sFlag = true; 293 if (pFlag) { 294 testPrintE("Invalid combination of command-line options."); 295 testPrintE(" The -p option is mutually exclusive from the"); 296 testPrintE(" -s and -e options."); 297 exit(5); 298 } 299 startPass = strtoul(optarg, &chptr, 10); 300 if (*chptr != '\0') { 301 testPrintE("Invalid command-line specified starting pass " 302 "of: %s", optarg); 303 exit(6); 304 } 305 break; 306 307 case 'e': // Ending Pass 308 eFlag = true; 309 if (pFlag) { 310 testPrintE("Invalid combination of command-line options."); 311 testPrintE(" The -p option is mutually exclusive from the"); 312 testPrintE(" -s and -e options."); 313 exit(7); 314 } 315 endPass = strtoul(optarg, &chptr, 10); 316 if (*chptr != '\0') { 317 testPrintE("Invalid command-line specified ending pass " 318 "of: %s", optarg); 319 exit(8); 320 } 321 break; 322 323 case 'p': // Run a single specified pass 324 pFlag = true; 325 if (sFlag || eFlag) { 326 testPrintE("Invalid combination of command-line options."); 327 testPrintE(" The -p option is mutually exclusive from the"); 328 testPrintE(" -s and -e options."); 329 exit(9); 330 } 331 startPass = endPass = strtoul(optarg, &chptr, 10); 332 if (*chptr != '\0') { 333 testPrintE("Invalid command-line specified pass of: %s", 334 optarg); 335 exit(10); 336 } 337 break; 338 339 case 'v': // Verbose 340 verbose = true; 341 break; 342 343 case 'h': // Help 344 case '?': 345 default: 346 testPrintE(" %s [options]", basename(argv[0])); 347 testPrintE(" options:"); 348 testPrintE(" -p Execute specified pass"); 349 testPrintE(" -s Starting pass"); 350 testPrintE(" -e Ending pass"); 351 testPrintE(" -t Duration"); 352 testPrintE(" -d Delay after each set operation"); 353 testPrintE(" -D End of test delay"); 354 testPrintE(" -n Num set operations per pass"); 355 testPrintE(" -v Verbose"); 356 exit(((optopt == 0) || (optopt == '?')) ? 0 : 11); 357 } 358 } 359 if (endPass < startPass) { 360 testPrintE("Unexpected ending pass before starting pass"); 361 testPrintE(" startPass: %u endPass: %u", startPass, endPass); 362 exit(12); 363 } 364 if (argc != optind) { 365 testPrintE("Unexpected command-line postional argument"); 366 testPrintE(" %s [-s start_pass] [-e end_pass] [-t duration]", 367 basename(argv[0])); 368 exit(13); 369 } 370 testPrintI("duration: %g", duration); 371 testPrintI("startPass: %u", startPass); 372 testPrintI("endPass: %u", endPass); 373 testPrintI("numSet: %u", numSet); 374 375 // Stop framework 376 rv = snprintf(cmd, sizeof(cmd), "%s", CMD_STOP_FRAMEWORK); 377 if (rv >= (signed) sizeof(cmd) - 1) { 378 testPrintE("Command too long for: %s", CMD_STOP_FRAMEWORK); 379 exit(14); 380 } 381 testExecCmd(cmd); 382 testDelay(1.0); // TODO - need means to query whether asyncronous stop 383 // framework operation has completed. For now, just wait 384 // a long time. 385 386 init(); 387 388 // For each pass 389 gettimeofday(&startTime, NULL); 390 for (pass = startPass; pass <= endPass; pass++) { 391 // Stop if duration of work has already been performed 392 gettimeofday(¤tTime, NULL); 393 delta = tvDelta(&startTime, ¤tTime); 394 if (tv2double(&delta) > duration) { break; } 395 396 // Regenerate a new set of test frames when this pass is 397 // either the first pass or is in a different group then 398 // the previous pass. A group of passes are passes that 399 // all have the same quotient when their pass number is 400 // divided by passesPerGroup. 401 if ((pass == startPass) 402 || ((pass / passesPerGroup) != ((pass - 1) / passesPerGroup))) { 403 initFrames(pass / passesPerGroup); 404 } 405 406 testPrintI("==== Starting pass: %u", pass); 407 408 // Cause deterministic sequence of prandom numbers to be 409 // generated for this pass. 410 srand48(pass); 411 412 hwc_layer_list_t *list; 413 list = hwcTestCreateLayerList(testRandMod(frames.size()) + 1); 414 if (list == NULL) { 415 testPrintE("hwcTestCreateLayerList failed"); 416 exit(20); 417 } 418 419 // Prandomly select a subset of frames to be used by this pass. 420 vector <vector <sp<GraphicBuffer> > > selectedFrames; 421 selectedFrames = vectorRandSelect(frames, list->numHwLayers); 422 423 // Any transform tends to create a layer that the hardware 424 // composer is unable to support and thus has to leave for 425 // SurfaceFlinger. Place heavy bias on specifying no transforms. 426 bool noTransform = testRandFract() > rareRatio; 427 428 for (unsigned int n1 = 0; n1 < list->numHwLayers; n1++) { 429 unsigned int idx = testRandMod(selectedFrames[n1].size()); 430 sp<GraphicBuffer> gBuf = selectedFrames[n1][idx]; 431 hwc_layer_t *layer = &list->hwLayers[n1]; 432 layer->handle = gBuf->handle; 433 434 layer->blending = blendingOps[testRandMod(NUMA(blendingOps))]; 435 layer->flags = (testRandFract() > rareRatio) ? 0 436 : vectorOr(vectorRandSelect(vecLayerFlags, 437 testRandMod(vecLayerFlags.size() + 1))); 438 layer->transform = (noTransform || testRandFract() > rareRatio) ? 0 439 : vectorOr(vectorRandSelect(vecTransformFlags, 440 testRandMod(vecTransformFlags.size() + 1))); 441 layer->sourceCrop.left = testRandMod(gBuf->getWidth()); 442 layer->sourceCrop.top = testRandMod(gBuf->getHeight()); 443 layer->sourceCrop.right = layer->sourceCrop.left 444 + testRandMod(gBuf->getWidth() - layer->sourceCrop.left) + 1; 445 layer->sourceCrop.bottom = layer->sourceCrop.top 446 + testRandMod(gBuf->getHeight() - layer->sourceCrop.top) + 1; 447 layer->displayFrame.left = testRandMod(width); 448 layer->displayFrame.top = testRandMod(height); 449 layer->displayFrame.right = layer->displayFrame.left 450 + testRandMod(width - layer->displayFrame.left) + 1; 451 layer->displayFrame.bottom = layer->displayFrame.top 452 + testRandMod(height - layer->displayFrame.top) + 1; 453 454 // Increase the frequency that a scale factor of 1.0 from 455 // the sourceCrop to displayFrame occurs. This is the 456 // most common scale factor used by applications and would 457 // be rarely produced by this stress test without this 458 // logic. 459 if (testRandFract() <= freqRatio) { 460 // Only change to scale factor to 1.0 if both the 461 // width and height will fit. 462 int sourceWidth = layer->sourceCrop.right 463 - layer->sourceCrop.left; 464 int sourceHeight = layer->sourceCrop.bottom 465 - layer->sourceCrop.top; 466 if (((layer->displayFrame.left + sourceWidth) <= width) 467 && ((layer->displayFrame.top + sourceHeight) <= height)) { 468 layer->displayFrame.right = layer->displayFrame.left 469 + sourceWidth; 470 layer->displayFrame.bottom = layer->displayFrame.top 471 + sourceHeight; 472 } 473 } 474 475 layer->visibleRegionScreen.numRects = 1; 476 layer->visibleRegionScreen.rects = &layer->displayFrame; 477 } 478 479 // Perform prepare operation 480 if (verbose) { testPrintI("Prepare:"); hwcTestDisplayList(list); } 481 hwcDevice->prepare(hwcDevice, list); 482 if (verbose) { 483 testPrintI("Post Prepare:"); 484 hwcTestDisplayListPrepareModifiable(list); 485 } 486 487 // Turn off the geometry changed flag 488 list->flags &= ~HWC_GEOMETRY_CHANGED; 489 490 // Perform the set operation(s) 491 if (verbose) {testPrintI("Set:"); } 492 for (unsigned int n1 = 0; n1 < numSet; n1++) { 493 if (verbose) { hwcTestDisplayListHandles(list); } 494 hwcDevice->set(hwcDevice, dpy, surface, list); 495 496 // Prandomly select a new set of handles 497 for (unsigned int n1 = 0; n1 < list->numHwLayers; n1++) { 498 unsigned int idx = testRandMod(selectedFrames[n1].size()); 499 sp<GraphicBuffer> gBuf = selectedFrames[n1][idx]; 500 hwc_layer_t *layer = &list->hwLayers[n1]; 501 layer->handle = (native_handle_t *) gBuf->handle; 502 } 503 504 testDelay(perSetDelay); 505 } 506 507 hwcTestFreeLayerList(list); 508 testPrintI("==== Completed pass: %u", pass); 509 } 510 511 testDelay(endDelay); 512 513 // Start framework 514 rv = snprintf(cmd, sizeof(cmd), "%s", CMD_START_FRAMEWORK); 515 if (rv >= (signed) sizeof(cmd) - 1) { 516 testPrintE("Command too long for: %s", CMD_START_FRAMEWORK); 517 exit(21); 518 } 519 testExecCmd(cmd); 520 521 testPrintI("Successfully completed %u passes", pass - startPass); 522 523 return 0; 524 } 525 526 void init(void) 527 { 528 srand48(0); // Defensively set pseudo random number generator. 529 // Should not need to set this, because a stress test 530 // sets the seed on each pass. Defensively set it here 531 // so that future code that uses pseudo random numbers 532 // before the first pass will be deterministic. 533 534 hwcTestInitDisplay(verbose, &dpy, &surface, &width, &height); 535 536 hwcTestOpenHwc(&hwcDevice); 537 } 538 539 /* 540 * Initialize Frames 541 * 542 * Creates an array of graphic buffers, within the global variable 543 * named frames. The graphic buffers are contained within a vector of 544 * vectors. All the graphic buffers in a particular row are of the same 545 * format and dimension. Each graphic buffer is uniformly filled with a 546 * prandomly selected color. It is likely that each buffer, even 547 * in the same row, will be filled with a unique color. 548 */ 549 void initFrames(unsigned int seed) 550 { 551 int rv; 552 const size_t maxRows = 5; 553 const size_t minCols = 2; // Need at least double buffering 554 const size_t maxCols = 4; // One more than triple buffering 555 556 if (verbose) { testPrintI("initFrames seed: %u", seed); } 557 srand48(seed); 558 size_t rows = testRandMod(maxRows) + 1; 559 560 frames.clear(); 561 frames.resize(rows); 562 563 for (unsigned int row = 0; row < rows; row++) { 564 // All frames within a row have to have the same format and 565 // dimensions. Width and height need to be >= 1. 566 unsigned int formatIdx = testRandMod(NUMA(hwcTestGraphicFormat)); 567 const struct hwcTestGraphicFormat *formatPtr 568 = &hwcTestGraphicFormat[formatIdx]; 569 int format = formatPtr->format; 570 571 // Pick width and height, which must be >= 1 and the size 572 // mod the wMod/hMod value must be equal to 0. 573 size_t w = (width * maxSizeRatio) * testRandFract(); 574 size_t h = (height * maxSizeRatio) * testRandFract(); 575 w = max(1u, w); 576 h = max(1u, h); 577 if ((w % formatPtr->wMod) != 0) { 578 w += formatPtr->wMod - (w % formatPtr->wMod); 579 } 580 if ((h % formatPtr->hMod) != 0) { 581 h += formatPtr->hMod - (h % formatPtr->hMod); 582 } 583 if (verbose) { 584 testPrintI(" frame %u width: %u height: %u format: %u %s", 585 row, w, h, format, hwcTestGraphicFormat2str(format)); 586 } 587 588 size_t cols = testRandMod((maxCols + 1) - minCols) + minCols; 589 frames[row].resize(cols); 590 for (unsigned int col = 0; col < cols; col++) { 591 ColorFract color(testRandFract(), testRandFract(), testRandFract()); 592 float alpha = testRandFract(); 593 594 frames[row][col] = new GraphicBuffer(w, h, format, texUsage); 595 if ((rv = frames[row][col]->initCheck()) != NO_ERROR) { 596 testPrintE("GraphicBuffer initCheck failed, rv: %i", rv); 597 testPrintE(" frame %u width: %u height: %u format: %u %s", 598 row, w, h, format, hwcTestGraphicFormat2str(format)); 599 exit(80); 600 } 601 602 hwcTestFillColor(frames[row][col].get(), color, alpha); 603 if (verbose) { 604 testPrintI(" buf: %p handle: %p color: %s alpha: %f", 605 frames[row][col].get(), frames[row][col]->handle, 606 string(color).c_str(), alpha); 607 } 608 } 609 } 610 } 611 612 /* 613 * Vector Random Select 614 * 615 * Prandomly selects and returns num elements from vec. 616 */ 617 template <class T> 618 vector<T> vectorRandSelect(const vector<T>& vec, size_t num) 619 { 620 vector<T> rv = vec; 621 622 while (rv.size() > num) { 623 rv.erase(rv.begin() + testRandMod(rv.size())); 624 } 625 626 return rv; 627 } 628 629 /* 630 * Vector Or 631 * 632 * Or's togethen the values of each element of vec and returns the result. 633 */ 634 template <class T> 635 T vectorOr(const vector<T>& vec) 636 { 637 T rv = 0; 638 639 for (size_t n1 = 0; n1 < vec.size(); n1++) { 640 rv |= vec[n1]; 641 } 642 643 return rv; 644 } 645