1/* 2 * Copyright (C) 2011 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 Color Equivalence 20 * 21 * Synopsis 22 * hwc_colorequiv [options] eFmt 23 * 24 * options: 25 -v - verbose 26 * -s <0.##, 0.##, 0.##> - Start color (default: <0.0, 0.0, 0.0> 27 * -e <0.##, 0.##, 0.##> - Ending color (default: <1.0, 1.0, 1.0> 28 * -r fmt - reference graphic format 29 * -D #.## - End of test delay 30 * 31 * graphic formats: 32 * RGBA8888 (reference frame default) 33 * RGBX8888 34 * RGB888 35 * RGB565 36 * BGRA8888 37 * RGBA5551 38 * RGBA4444 39 * YV12 40 * 41 * Description 42 * Renders a horizontal blend in two frames. The first frame is rendered 43 * in the upper third of the display and is called the reference frame. 44 * The second frame is displayed in the middle third and is called the 45 * equivalence frame. The primary purpose of this utility is to verify 46 * that the colors produced in the reference and equivalence frames are 47 * the same. The colors are the same when the colors are the same 48 * vertically between the reference and equivalence frames. 49 * 50 * By default the reference frame is rendered through the use of the 51 * RGBA8888 graphic format. The -r option can be used to specify a 52 * non-default reference frame graphic format. The graphic format of 53 * the equivalence frame is determined by a single required positional 54 * parameter. Intentionally there is no default for the graphic format 55 * of the equivalence frame. 56 * 57 * The horizontal blend in the reference frame is produced from a linear 58 * interpolation from a start color (default: <0.0, 0.0, 0.0> on the left 59 * side to an end color (default <1.0, 1.0, 1.0> on the right side. Where 60 * possible the equivalence frame is rendered with the equivalent color 61 * from the reference frame. A color of black is used in the equivalence 62 * frame for cases where an equivalent color does not exist. 63 */ 64 65#include <algorithm> 66#include <assert.h> 67#include <cerrno> 68#include <cmath> 69#include <cstdlib> 70#include <ctime> 71#include <libgen.h> 72#include <sched.h> 73#include <sstream> 74#include <stdint.h> 75#include <string.h> 76#include <unistd.h> 77#include <vector> 78 79#include <sys/syscall.h> 80#include <sys/types.h> 81#include <sys/wait.h> 82 83#include <EGL/egl.h> 84#include <EGL/eglext.h> 85#include <GLES2/gl2.h> 86#include <GLES2/gl2ext.h> 87 88#include <ui/GraphicBuffer.h> 89 90#define LOG_TAG "hwcColorEquivTest" 91#include <utils/Log.h> 92#include <testUtil.h> 93 94#include <hardware/hwcomposer.h> 95 96#include "hwcTestLib.h" 97 98using namespace std; 99using namespace android; 100 101// Defaults for command-line options 102const bool defaultVerbose = false; 103const ColorFract defaultStartColor(0.0, 0.0, 0.0); 104const ColorFract defaultEndColor(1.0, 1.0, 1.0); 105const char *defaultRefFormat = "RGBA8888"; 106const float defaultEndDelay = 2.0; // Default delay after rendering graphics 107 108// Defines 109#define MAXSTR 100 110#define MAXCMD 200 111#define BITSPERBYTE 8 // TODO: Obtain from <values.h>, once 112 // it has been added 113 114#define CMD_STOP_FRAMEWORK "stop 2>&1" 115#define CMD_START_FRAMEWORK "start 2>&1" 116 117// Macros 118#define NUMA(a) (sizeof(a) / sizeof(a [0])) // Num elements in an array 119#define MEMCLR(addr, size) do { \ 120 memset((addr), 0, (size)); \ 121 } while (0) 122 123// Globals 124static const int texUsage = GraphicBuffer::USAGE_HW_TEXTURE | 125 GraphicBuffer::USAGE_SW_WRITE_RARELY; 126static hwc_composer_device_1_t *hwcDevice; 127static EGLDisplay dpy; 128static EGLSurface surface; 129static EGLint width, height; 130 131// Functions prototypes 132void init(void); 133void printSyntax(const char *cmd); 134 135// Command-line option settings 136static bool verbose = defaultVerbose; 137static ColorFract startRefColor = defaultStartColor; 138static ColorFract endRefColor = defaultEndColor; 139static float endDelay = defaultEndDelay; 140static const struct hwcTestGraphicFormat *refFormat 141 = hwcTestGraphicFormatLookup(defaultRefFormat); 142static const struct hwcTestGraphicFormat *equivFormat; 143 144/* 145 * Main 146 * 147 * Performs the following high-level sequence of operations: 148 * 149 * 1. Command-line parsing 150 * 151 * 2. Stop framework 152 * 153 * 3. Initialization 154 * 155 * 4. Create Hardware Composer description of reference and equivalence frames 156 * 157 * 5. Have Hardware Composer render the reference and equivalence frames 158 * 159 * 6. Delay for amount of time given by endDelay 160 * 161 * 7. Start framework 162 */ 163int 164main(int argc, char *argv[]) 165{ 166 int rv, opt; 167 bool error; 168 char *chptr; 169 unsigned int pass; 170 char cmd[MAXCMD]; 171 string str; 172 173 testSetLogCatTag(LOG_TAG); 174 175 assert(refFormat != NULL); 176 177 testSetLogCatTag(LOG_TAG); 178 179 // Parse command line arguments 180 while ((opt = getopt(argc, argv, "vs:e:r:D:?h")) != -1) { 181 switch (opt) { 182 case 'D': // End of test delay 183 // Delay between completion of final pass and restart 184 // of framework 185 endDelay = strtod(optarg, &chptr); 186 if ((*chptr != '\0') || (endDelay < 0.0)) { 187 testPrintE("Invalid command-line specified end of test delay " 188 "of: %s", optarg); 189 exit(1); 190 } 191 break; 192 193 case 's': // Starting reference color 194 str = optarg; 195 while (optind < argc) { 196 if (*argv[optind] == '-') { break; } 197 char endChar = (str.length() > 1) ? str[str.length() - 1] : 0; 198 if ((endChar == '>') || (endChar == ']')) { break; } 199 str += " " + string(argv[optind++]); 200 } 201 { 202 istringstream in(str); 203 startRefColor = hwcTestParseColor(in, error); 204 // Any parse error or characters not used by parser 205 if (error 206 || (((unsigned int) in.tellg() != in.str().length()) 207 && (in.tellg() != (streampos) -1))) { 208 testPrintE("Invalid command-line specified start " 209 "reference color of: %s", str.c_str()); 210 exit(2); 211 } 212 } 213 break; 214 215 case 'e': // Ending reference color 216 str = optarg; 217 while (optind < argc) { 218 if (*argv[optind] == '-') { break; } 219 char endChar = (str.length() > 1) ? str[str.length() - 1] : 0; 220 if ((endChar == '>') || (endChar == ']')) { break; } 221 str += " " + string(argv[optind++]); 222 } 223 { 224 istringstream in(str); 225 endRefColor = hwcTestParseColor(in, error); 226 // Any parse error or characters not used by parser 227 if (error 228 || (((unsigned int) in.tellg() != in.str().length()) 229 && (in.tellg() != (streampos) -1))) { 230 testPrintE("Invalid command-line specified end " 231 "reference color of: %s", str.c_str()); 232 exit(3); 233 } 234 } 235 break; 236 237 case 'r': // Reference graphic format 238 refFormat = hwcTestGraphicFormatLookup(optarg); 239 if (refFormat == NULL) { 240 testPrintE("Unkown command-line specified reference graphic " 241 "format of: %s", optarg); 242 printSyntax(basename(argv[0])); 243 exit(4); 244 } 245 break; 246 247 case 'v': // Verbose 248 verbose = true; 249 break; 250 251 case 'h': // Help 252 case '?': 253 default: 254 printSyntax(basename(argv[0])); 255 exit(((optopt == 0) || (optopt == '?')) ? 0 : 5); 256 } 257 } 258 259 // Expect a single positional parameter, which specifies the 260 // equivalence graphic format. 261 if (argc != (optind + 1)) { 262 testPrintE("Expected a single command-line postional parameter"); 263 printSyntax(basename(argv[0])); 264 exit(6); 265 } 266 equivFormat = hwcTestGraphicFormatLookup(argv[optind]); 267 if (equivFormat == NULL) { 268 testPrintE("Unkown command-line specified equivalence graphic " 269 "format of: %s", argv[optind]); 270 printSyntax(basename(argv[0])); 271 exit(7); 272 } 273 274 testPrintI("refFormat: %u %s", refFormat->format, refFormat->desc); 275 testPrintI("equivFormat: %u %s", equivFormat->format, equivFormat->desc); 276 testPrintI("startRefColor: %s", ((string) startRefColor).c_str()); 277 testPrintI("endRefColor: %s", ((string) endRefColor).c_str()); 278 testPrintI("endDelay: %f", endDelay); 279 280 // Stop framework 281 rv = snprintf(cmd, sizeof(cmd), "%s", CMD_STOP_FRAMEWORK); 282 if (rv >= (signed) sizeof(cmd) - 1) { 283 testPrintE("Command too long for: %s", CMD_STOP_FRAMEWORK); 284 exit(8); 285 } 286 testExecCmd(cmd); 287 testDelay(1.0); // TODO - needs means to query whether asynchronous stop 288 // framework operation has completed. For now, just wait 289 // a long time. 290 291 init(); 292 293 // Use the upper third of the display for the reference frame and 294 // the middle third for the equivalence frame. 295 unsigned int refHeight = height / 3; 296 unsigned int refPosY = 0; // Reference frame Y position 297 unsigned int refPosX = 0; // Reference frame X position 298 unsigned int refWidth = width - refPosX; 299 if ((refWidth & refFormat->wMod) != 0) { 300 refWidth += refFormat->wMod - (refWidth % refFormat->wMod); 301 } 302 unsigned int equivHeight = height / 3; 303 unsigned int equivPosY = refHeight; // Equivalence frame Y position 304 unsigned int equivPosX = 0; // Equivalence frame X position 305 unsigned int equivWidth = width - equivPosX; 306 if ((equivWidth & equivFormat->wMod) != 0) { 307 equivWidth += equivFormat->wMod - (equivWidth % equivFormat->wMod); 308 } 309 310 // Create reference and equivalence graphic buffers 311 const unsigned int numFrames = 2; 312 sp<GraphicBuffer> refFrame; 313 refFrame = new GraphicBuffer(refWidth, refHeight, 314 refFormat->format, texUsage); 315 if ((rv = refFrame->initCheck()) != NO_ERROR) { 316 testPrintE("refFrame initCheck failed, rv: %i", rv); 317 testPrintE(" width %u height: %u format: %u %s", refWidth, refHeight, 318 refFormat->format, 319 hwcTestGraphicFormat2str(refFormat->format)); 320 exit(9); 321 } 322 testPrintI("refFrame width: %u height: %u format: %u %s", 323 refWidth, refHeight, refFormat->format, 324 hwcTestGraphicFormat2str(refFormat->format)); 325 326 sp<GraphicBuffer> equivFrame; 327 equivFrame = new GraphicBuffer(equivWidth, equivHeight, 328 equivFormat->format, texUsage); 329 if ((rv = refFrame->initCheck()) != NO_ERROR) { 330 testPrintE("refFrame initCheck failed, rv: %i", rv); 331 testPrintE(" width %u height: %u format: %u %s", refWidth, refHeight, 332 refFormat->format, 333 hwcTestGraphicFormat2str(refFormat->format)); 334 exit(10); 335 } 336 testPrintI("equivFrame width: %u height: %u format: %u %s", 337 equivWidth, equivHeight, equivFormat->format, 338 hwcTestGraphicFormat2str(equivFormat->format)); 339 340 // Fill the frames with a horizontal blend 341 hwcTestFillColorHBlend(refFrame.get(), refFormat->format, 342 startRefColor, endRefColor); 343 hwcTestFillColorHBlend(equivFrame.get(), refFormat->format, 344 startRefColor, endRefColor); 345 346 hwc_display_contents_1_t *list; 347 size_t size = sizeof(hwc_display_contents_1_t) + numFrames * sizeof(hwc_layer_1_t); 348 if ((list = (hwc_display_contents_1_t *) calloc(1, size)) == NULL) { 349 testPrintE("Allocate list failed"); 350 exit(11); 351 } 352 list->flags = HWC_GEOMETRY_CHANGED; 353 list->numHwLayers = numFrames; 354 355 hwc_layer_1_t *layer = &list->hwLayers[0]; 356 layer->handle = refFrame->handle; 357 layer->blending = HWC_BLENDING_NONE; 358 layer->sourceCrop.left = 0; 359 layer->sourceCrop.top = 0; 360 layer->sourceCrop.right = width; 361 layer->sourceCrop.bottom = refHeight; 362 layer->displayFrame.left = 0; 363 layer->displayFrame.top = 0; 364 layer->displayFrame.right = width; 365 layer->displayFrame.bottom = refHeight; 366 layer->visibleRegionScreen.numRects = 1; 367 layer->visibleRegionScreen.rects = &layer->displayFrame; 368 369 layer++; 370 layer->handle = equivFrame->handle; 371 layer->blending = HWC_BLENDING_NONE; 372 layer->sourceCrop.left = 0; 373 layer->sourceCrop.top = 0; 374 layer->sourceCrop.right = width; 375 layer->sourceCrop.bottom = equivHeight; 376 layer->displayFrame.left = 0; 377 layer->displayFrame.top = refHeight; 378 layer->displayFrame.right = width; 379 layer->displayFrame.bottom = layer->displayFrame.top + equivHeight; 380 layer->visibleRegionScreen.numRects = 1; 381 layer->visibleRegionScreen.rects = &layer->displayFrame; 382 383 // Perform prepare operation 384 if (verbose) { testPrintI("Prepare:"); hwcTestDisplayList(list); } 385 hwcDevice->prepare(hwcDevice, 1, &list); 386 if (verbose) { 387 testPrintI("Post Prepare:"); 388 hwcTestDisplayListPrepareModifiable(list); 389 } 390 391 // Turn off the geometry changed flag 392 list->flags &= ~HWC_GEOMETRY_CHANGED; 393 394 if (verbose) {hwcTestDisplayListHandles(list); } 395 list->dpy = dpy; 396 list->sur = surface; 397 hwcDevice->set(hwcDevice, 1, &list); 398 399 testDelay(endDelay); 400 401 // Start framework 402 rv = snprintf(cmd, sizeof(cmd), "%s", CMD_START_FRAMEWORK); 403 if (rv >= (signed) sizeof(cmd) - 1) { 404 testPrintE("Command too long for: %s", CMD_START_FRAMEWORK); 405 exit(12); 406 } 407 testExecCmd(cmd); 408 409 return 0; 410} 411 412void init(void) 413{ 414 // Seed pseudo random number generator 415 // Seeding causes fill horizontal blend to fill the pad area with 416 // a deterministic set of values. 417 srand48(0); 418 419 hwcTestInitDisplay(verbose, &dpy, &surface, &width, &height); 420 421 hwcTestOpenHwc(&hwcDevice); 422} 423 424void printSyntax(const char *cmd) 425{ 426 testPrintE(" %s [options] graphicFormat", cmd); 427 testPrintE(" options:"); 428 testPrintE(" -s <0.##, 0.##, 0.##> - Starting reference color"); 429 testPrintE(" -e <0.##, 0.##, 0.##> - Ending reference color"); 430 testPrintE(" -r format - Reference graphic format"); 431 testPrintE(" -D #.## - End of test delay"); 432 testPrintE(" -v Verbose"); 433 testPrintE(""); 434 testPrintE(" graphic formats:"); 435 for (unsigned int n1 = 0; n1 < NUMA(hwcTestGraphicFormat); n1++) { 436 testPrintE(" %s", hwcTestGraphicFormat[n1].desc); 437 } 438} 439