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