blob: 575af8993cd3c75f6b5e152bb3167d46829993af [file] [log] [blame]
Louis Huemillerec0da1a2011-01-05 18:53:47 -08001/*
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 Test Library
20 * Utility library functions for use by the Hardware Composer test cases
21 */
22
23#include <sstream>
24#include <string>
25
26#include <arpa/inet.h> // For ntohl() and htonl()
27
28#include <hwc/hwcTestLib.h>
29
30// Defines
31#define NUMA(a) (sizeof(a) / sizeof(a [0]))
32
33// Function Prototypes
34static void printGLString(const char *name, GLenum s);
35static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE);
36static void checkGlError(const char* op);
37static void printEGLConfiguration(EGLDisplay dpy, EGLConfig config);
38
39using namespace std;
40using namespace android;
41
42
43#define BITSPERBYTE 8 // TODO: Obtain from <values.h>, once
44 // it has been added
45
46// Initialize Display
47void hwcTestInitDisplay(bool verbose, EGLDisplay *dpy, EGLSurface *surface,
48 EGLint *width, EGLint *height)
49{
50 static EGLContext context;
51
52 int rv;
53
54 EGLBoolean returnValue;
55 EGLConfig myConfig = {0};
56 EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
57 EGLint sConfigAttribs[] = {
58 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
59 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
60 EGL_NONE };
61 EGLint majorVersion, minorVersion;
62
63 checkEglError("<init>");
64 *dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
65 checkEglError("eglGetDisplay");
66 if (*dpy == EGL_NO_DISPLAY) {
67 testPrintE("eglGetDisplay returned EGL_NO_DISPLAY");
68 exit(70);
69 }
70
71 returnValue = eglInitialize(*dpy, &majorVersion, &minorVersion);
72 checkEglError("eglInitialize", returnValue);
73 if (verbose) {
74 testPrintI("EGL version %d.%d", majorVersion, minorVersion);
75 }
76 if (returnValue != EGL_TRUE) {
77 testPrintE("eglInitialize failed");
78 exit(71);
79 }
80
81 EGLNativeWindowType window = android_createDisplaySurface();
82 if (window == NULL) {
83 testPrintE("android_createDisplaySurface failed");
84 exit(72);
85 }
86 returnValue = EGLUtils::selectConfigForNativeWindow(*dpy,
87 sConfigAttribs, window, &myConfig);
88 if (returnValue) {
89 testPrintE("EGLUtils::selectConfigForNativeWindow() returned %d",
90 returnValue);
91 exit(73);
92 }
93 checkEglError("EGLUtils::selectConfigForNativeWindow");
94
95 if (verbose) {
96 testPrintI("Chose this configuration:");
97 printEGLConfiguration(*dpy, myConfig);
98 }
99
100 *surface = eglCreateWindowSurface(*dpy, myConfig, window, NULL);
101 checkEglError("eglCreateWindowSurface");
102 if (*surface == EGL_NO_SURFACE) {
103 testPrintE("gelCreateWindowSurface failed.");
104 exit(74);
105 }
106
107 context = eglCreateContext(*dpy, myConfig, EGL_NO_CONTEXT, contextAttribs);
108 checkEglError("eglCreateContext");
109 if (context == EGL_NO_CONTEXT) {
110 testPrintE("eglCreateContext failed");
111 exit(75);
112 }
113 returnValue = eglMakeCurrent(*dpy, *surface, *surface, context);
114 checkEglError("eglMakeCurrent", returnValue);
115 if (returnValue != EGL_TRUE) {
116 testPrintE("eglMakeCurrent failed");
117 exit(76);
118 }
119 eglQuerySurface(*dpy, *surface, EGL_WIDTH, width);
120 checkEglError("eglQuerySurface");
121 eglQuerySurface(*dpy, *surface, EGL_HEIGHT, height);
122 checkEglError("eglQuerySurface");
123
124 if (verbose) {
125 testPrintI("Window dimensions: %d x %d", *width, *height);
126
127 printGLString("Version", GL_VERSION);
128 printGLString("Vendor", GL_VENDOR);
129 printGLString("Renderer", GL_RENDERER);
130 printGLString("Extensions", GL_EXTENSIONS);
131 }
132}
133
134// Open Hardware Composer Device
135void hwcTestOpenHwc(hwc_composer_device_t **hwcDevicePtr)
136{
137 int rv;
138 hw_module_t const *hwcModule;
139
140 if ((rv = hw_get_module(HWC_HARDWARE_MODULE_ID, &hwcModule)) != 0) {
141 testPrintE("hw_get_module failed, rv: %i", rv);
142 errno = -rv;
143 perror(NULL);
144 exit(77);
145 }
146 if ((rv = hwc_open(hwcModule, hwcDevicePtr)) != 0) {
147 testPrintE("hwc_open failed, rv: %i", rv);
148 errno = -rv;
149 perror(NULL);
150 exit(78);
151 }
152}
153
154// Color fraction class to string conversion
155ColorFract::operator string()
156{
157 ostringstream out;
158
159 out << '[' << this->c1() << ", "
160 << this->c2() << ", "
161 << this->c3() << ']';
162
163 return out.str();
164}
165
166// Dimension class to string conversion
167HwcTestDim::operator string()
168{
169 ostringstream out;
170
171 out << '[' << this->width() << ", "
172 << this->height() << ']';
173
174 return out.str();
175}
176
177// Hardware Composer rectangle to string conversion
178string hwcTestRect2str(const struct hwc_rect& rect)
179{
180 ostringstream out;
181
182 out << '[';
183 out << rect.left << ", ";
184 out << rect.top << ", ";
185 out << rect.right << ", ";
186 out << rect.bottom;
187 out << ']';
188
189 return out.str();
190}
191
192// Parse HWC rectangle description of form [left, top, right, bottom]
193struct hwc_rect hwcTestParseHwcRect(istringstream& in, bool& error)
194{
195 struct hwc_rect rect;
196 char chStart, ch;
197
198 // Defensively specify that an error occurred. Will clear
199 // error flag if all of parsing succeeds.
200 error = true;
201
202 // First character should be a [ or <
203 in >> chStart;
204 if (!in || ((chStart != '<') && (chStart != '['))) { return rect; }
205
206 // Left
207 in >> rect.left;
208 if (!in) { return rect; }
209 in >> ch;
210 if (!in || (ch != ',')) { return rect; }
211
212 // Top
213 in >> rect.top;
214 if (!in) { return rect; }
215 in >> ch;
216 if (!in || (ch != ',')) { return rect; }
217
218 // Right
219 in >> rect.right;
220 if (!in) { return rect; }
221 in >> ch;
222 if (!in || (ch != ',')) { return rect; }
223
224 // Bottom
225 in >> rect.bottom;
226 if (!in) { return rect; }
227
228 // Closing > or ]
229 in >> ch;
230 if (!in) { return rect; }
231 if (((chStart == '<') && (ch != '>'))
232 || ((chStart == '[') && (ch != ']'))) { return rect; }
233
234 // Validate right and bottom are greater than left and top
235 if ((rect.right <= rect.left) || (rect.bottom <= rect.top)) { return rect; }
236
237 // Made It, clear error indicator
238 error = false;
239
240 return rect;
241}
242
243// Parse dimension of form [width, height]
244HwcTestDim hwcTestParseDim(istringstream& in, bool& error)
245{
246 HwcTestDim dim;
247 char chStart, ch;
248 uint32_t val;
249
250 // Defensively specify that an error occurred. Will clear
251 // error flag if all of parsing succeeds.
252 error = true;
253
254 // First character should be a [ or <
255 in >> chStart;
256 if (!in || ((chStart != '<') && (chStart != '['))) { return dim; }
257
258 // Width
259 in >> val;
260 if (!in) { return dim; }
261 dim.setWidth(val);
262 in >> ch;
263 if (!in || (ch != ',')) { return dim; }
264
265 // Height
266 in >> val;
267 if (!in) { return dim; }
268 dim.setHeight(val);
269
270 // Closing > or ]
271 in >> ch;
272 if (!in) { return dim; }
273 if (((chStart == '<') && (ch != '>'))
274 || ((chStart == '[') && (ch != ']'))) { return dim; }
275
276 // Validate width and height greater than 0
277 if ((dim.width() <= 0) || (dim.height() <= 0)) { return dim; }
278
279 // Made It, clear error indicator
280 error = false;
281 return dim;
282}
283
284// Parse fractional color of form [0.##, 0.##, 0.##]
285// Fractional values can be from 0.0 to 1.0 inclusive. Note, integer
286// values of 0.0 and 1.0, which are non-fractional, are considered valid.
287// They are an exception, all other valid inputs are fractions.
288ColorFract hwcTestParseColor(istringstream& in, bool& error)
289{
290 ColorFract color;
291 char chStart, ch;
292 float c1, c2, c3;
293
294 // Defensively specify that an error occurred. Will clear
295 // error flag if all of parsing succeeds.
296 error = true;
297
298 // First character should be a [ or <
299 in >> chStart;
300 if (!in || ((chStart != '<') && (chStart != '['))) { return color; }
301
302 // 1st Component
303 in >> c1;
304 if (!in) { return color; }
305 if ((c1 < 0.0) || (c1 > 1.0)) { return color; }
306 in >> ch;
307 if (!in || (ch != ',')) { return color; }
308
309 // 2nd Component
310 in >> c2;
311 if (!in) { return color; }
312 if ((c2 < 0.0) || (c2 > 1.0)) { return color; }
313 in >> ch;
314 if (!in || (ch != ',')) { return color; }
315
316 // 3rd Component
317 in >> c3;
318 if (!in) { return color; }
319 if ((c3 < 0.0) || (c3 > 1.0)) { return color; }
320
321 // Closing > or ]
322 in >> ch;
323 if (!in) { return color; }
324 if (((chStart == '<') && (ch != '>'))
325 || ((chStart == '[') && (ch != ']'))) { return color; }
326
327 // Are all the components fractional
328 if ((c1 < 0.0) || (c1 > 1.0)
329 || (c2 < 0.0) || (c2 > 1.0)
330 || (c3 < 0.0) || (c3 > 1.0)) { return color; }
331
332 // Made It, clear error indicator
333 error = false;
334
335 return ColorFract(c1, c2, c3);
336}
337
338// Look up and return pointer to structure with the characteristics
339// of the graphic format named by the desc parameter. Search failure
340// indicated by the return of NULL.
341const struct hwcTestGraphicFormat *hwcTestGraphicFormatLookup(const char *desc)
342{
343 for (unsigned int n1 = 0; n1 < NUMA(hwcTestGraphicFormat); n1++) {
344 if (string(desc) == string(hwcTestGraphicFormat[n1].desc)) {
345 return &hwcTestGraphicFormat[n1];
346 }
347 }
348
349 return NULL;
350}
351
352// Given the integer ID of a graphic format, return a pointer to
353// a string that describes the format.
354const char *hwcTestGraphicFormat2str(uint32_t format)
355{
356 const static char *unknown = "unknown";
357
358 for (unsigned int n1 = 0; n1 < NUMA(hwcTestGraphicFormat); n1++) {
359 if (format == hwcTestGraphicFormat[n1].format) {
360 return hwcTestGraphicFormat[n1].desc;
361 }
362 }
363
364 return unknown;
365}
366
367/*
368 * hwcTestCreateLayerList
369 * Dynamically creates layer list with numLayers worth
370 * of hwLayers entries.
371 */
372hwc_layer_list_t *hwcTestCreateLayerList(size_t numLayers)
373{
374 hwc_layer_list_t *list;
375
376 size_t size = sizeof(hwc_layer_list) + numLayers * sizeof(hwc_layer_t);
377 if ((list = (hwc_layer_list_t *) calloc(1, size)) == NULL) {
378 return NULL;
379 }
380 list->flags = HWC_GEOMETRY_CHANGED;
381 list->numHwLayers = numLayers;
382
383 return list;
384}
385
386/*
387 * hwcTestFreeLayerList
388 * Frees memory previous allocated via hwcTestCreateLayerList().
389 */
390void hwcTestFreeLayerList(hwc_layer_list_t *list)
391{
392 free(list);
393}
394
395// Display the settings of the layer list pointed to by list
396void hwcTestDisplayList(hwc_layer_list_t *list)
397{
398 testPrintI(" flags: %#x%s", list->flags,
399 (list->flags & HWC_GEOMETRY_CHANGED) ? " GEOMETRY_CHANGED" : "");
400 testPrintI(" numHwLayers: %u", list->numHwLayers);
401
402 for (unsigned int layer = 0; layer < list->numHwLayers; layer++) {
403 testPrintI(" layer %u compositionType: %#x%s%s", layer,
404 list->hwLayers[layer].compositionType,
405 (list->hwLayers[layer].compositionType == HWC_FRAMEBUFFER)
406 ? " FRAMEBUFFER" : "",
407 (list->hwLayers[layer].compositionType == HWC_OVERLAY)
408 ? " OVERLAY" : "");
409
410 testPrintI(" hints: %#x",
411 list->hwLayers[layer].hints,
412 (list->hwLayers[layer].hints & HWC_HINT_TRIPLE_BUFFER)
413 ? " TRIPLE_BUFFER" : "",
414 (list->hwLayers[layer].hints & HWC_HINT_CLEAR_FB)
415 ? " CLEAR_FB" : "");
416
417 testPrintI(" flags: %#x%s",
418 list->hwLayers[layer].flags,
419 (list->hwLayers[layer].flags & HWC_SKIP_LAYER)
420 ? " SKIP_LAYER" : "");
421
422 testPrintI(" handle: %p",
423 list->hwLayers[layer].handle);
424
425 // Intentionally skipped display of ROT_180 & ROT_270,
426 // which are formed from combinations of the other flags.
427 testPrintI(" transform: %#x%s%s%s",
428 list->hwLayers[layer].transform,
429 (list->hwLayers[layer].transform & HWC_TRANSFORM_FLIP_H)
430 ? " FLIP_H" : "",
431 (list->hwLayers[layer].transform & HWC_TRANSFORM_FLIP_V)
432 ? " FLIP_V" : "",
433 (list->hwLayers[layer].transform & HWC_TRANSFORM_ROT_90)
434 ? " ROT_90" : "");
435
436 testPrintI(" blending: %#x%s%s%s",
437 list->hwLayers[layer].blending,
438 (list->hwLayers[layer].blending == HWC_BLENDING_NONE)
439 ? " NONE" : "",
440 (list->hwLayers[layer].blending == HWC_BLENDING_PREMULT)
441 ? " PREMULT" : "",
442 (list->hwLayers[layer].blending == HWC_BLENDING_COVERAGE)
443 ? " COVERAGE" : "");
444
445 testPrintI(" sourceCrop: %s",
446 hwcTestRect2str(list->hwLayers[layer].sourceCrop).c_str());
447 testPrintI(" displayFrame: %s",
448 hwcTestRect2str(list->hwLayers[layer].displayFrame).c_str());
449 testPrintI(" scaleFactor: [%f, %f]",
450 (float) (list->hwLayers[layer].displayFrame.right
451 - list->hwLayers[layer].displayFrame.left)
452 / (float) (list->hwLayers[layer].sourceCrop.right
453 - list->hwLayers[layer].sourceCrop.left),
454 (float) (list->hwLayers[layer].displayFrame.bottom
455 - list->hwLayers[layer].displayFrame.top)
456 / (float) (list->hwLayers[layer].sourceCrop.bottom
457 - list->hwLayers[layer].sourceCrop.top));
458 }
459}
460
461/*
462 * Display List Prepare Modifiable
463 *
464 * Displays the portions of a list that are meant to be modified by
465 * a prepare call.
466 */
467void hwcTestDisplayListPrepareModifiable(hwc_layer_list_t *list)
468{
469 for (unsigned int layer = 0; layer < list->numHwLayers; layer++) {
470 testPrintI(" layer %u compositionType: %#x%s%s", layer,
471 list->hwLayers[layer].compositionType,
472 (list->hwLayers[layer].compositionType == HWC_FRAMEBUFFER)
473 ? " FRAMEBUFFER" : "",
474 (list->hwLayers[layer].compositionType == HWC_OVERLAY)
475 ? " OVERLAY" : "");
476 testPrintI(" hints: %#x%s%s",
477 list->hwLayers[layer].hints,
478 (list->hwLayers[layer].hints & HWC_HINT_TRIPLE_BUFFER)
479 ? " TRIPLE_BUFFER" : "",
480 (list->hwLayers[layer].hints & HWC_HINT_CLEAR_FB)
481 ? " CLEAR_FB" : "");
482 }
483}
484
485/*
486 * Display List Handles
487 *
488 * Displays the handles of all the graphic buffers in the list.
489 */
490void hwcTestDisplayListHandles(hwc_layer_list_t *list)
491{
492 const unsigned int maxLayersPerLine = 6;
493
494 ostringstream str(" layers:");
495 for (unsigned int layer = 0; layer < list->numHwLayers; layer++) {
496 str << ' ' << list->hwLayers[layer].handle;
497 if (((layer % maxLayersPerLine) == (maxLayersPerLine - 1))
498 && (layer != list->numHwLayers - 1)) {
499 testPrintI("%s", str.str().c_str());
500 str.str(" ");
501 }
502 }
503 testPrintI("%s", str.str().c_str());
504}
505
506// Returns a uint32_t that contains a format specific representation of a
507// single pixel of the given color and alpha values.
508uint32_t hwcTestColor2Pixel(uint32_t format, ColorFract color, float alpha)
509{
510 const struct attrib {
511 uint32_t format;
512 bool hostByteOrder;
513 size_t bytes;
514 size_t c1Offset;
515 size_t c1Size;
516 size_t c2Offset;
517 size_t c2Size;
518 size_t c3Offset;
519 size_t c3Size;
520 size_t aOffset;
521 size_t aSize;
522 } attributes[] = {
523 {HAL_PIXEL_FORMAT_RGBA_8888, false, 4, 0, 8, 8, 8, 16, 8, 24, 8},
524 {HAL_PIXEL_FORMAT_RGBX_8888, false, 4, 0, 8, 8, 8, 16, 8, 0, 0},
525 {HAL_PIXEL_FORMAT_RGB_888, false, 3, 0, 8, 8, 8, 16, 8, 0, 0},
526 {HAL_PIXEL_FORMAT_RGB_565, true, 2, 0, 5, 5, 6, 11, 5, 0, 0},
527 {HAL_PIXEL_FORMAT_BGRA_8888, false, 4, 16, 8, 8, 8, 0, 8, 24, 8},
528 {HAL_PIXEL_FORMAT_RGBA_5551, true , 2, 0, 5, 5, 5, 10, 5, 15, 1},
529 {HAL_PIXEL_FORMAT_RGBA_4444, false, 2, 12, 4, 0, 4, 4, 4, 8, 4},
530 {HAL_PIXEL_FORMAT_YV12, true, 3, 16, 8, 8, 8, 0, 8, 0, 0},
531 };
532
533 const struct attrib *attrib;
534 for (attrib = attributes; attrib < attributes + NUMA(attributes);
535 attrib++) {
536 if (attrib->format == format) { break; }
537 }
538 if (attrib >= attributes + NUMA(attributes)) {
539 testPrintE("colorFract2Pixel unsupported format of: %u", format);
540 exit(80);
541 }
542
543 uint32_t pixel;
544 pixel = htonl((uint32_t) round((((1 << attrib->c1Size) - 1) * color.c1()))
545 << ((sizeof(pixel) * BITSPERBYTE)
546 - (attrib->c1Offset + attrib->c1Size)));
547 pixel |= htonl((uint32_t) round((((1 << attrib->c2Size) - 1) * color.c2()))
548 << ((sizeof(pixel) * BITSPERBYTE)
549 - (attrib->c2Offset + attrib->c2Size)));
550 pixel |= htonl((uint32_t) round((((1 << attrib->c3Size) - 1) * color.c3()))
551 << ((sizeof(pixel) * BITSPERBYTE)
552 - (attrib->c3Offset + attrib->c3Size)));
553 if (attrib->aSize) {
554 pixel |= htonl((uint32_t) round((((1 << attrib->aSize) - 1) * alpha))
555 << ((sizeof(pixel) * BITSPERBYTE)
556 - (attrib->aOffset + attrib->aSize)));
557 }
558 if (attrib->hostByteOrder) {
559 pixel = ntohl(pixel);
560 pixel >>= sizeof(pixel) * BITSPERBYTE - attrib->bytes * BITSPERBYTE;
561 }
562
563 return pixel;
564}
565
566// Sets the pixel at the given x and y coordinates to the color and alpha
567// value given by pixel. The contents of pixel is format specific. It's
568// value should come from a call to hwcTestColor2Pixel().
569void hwcTestSetPixel(GraphicBuffer *gBuf, unsigned char *buf,
570 uint32_t x, uint32_t y, uint32_t pixel)
571{
572
573 const struct attrib {
574 int format;
575 size_t bytes;
576 } attributes[] = {
577 {HAL_PIXEL_FORMAT_RGBA_8888, 4},
578 {HAL_PIXEL_FORMAT_RGBX_8888, 4},
579 {HAL_PIXEL_FORMAT_RGB_888, 3},
580 {HAL_PIXEL_FORMAT_RGB_565, 2},
581 {HAL_PIXEL_FORMAT_BGRA_8888, 4},
582 {HAL_PIXEL_FORMAT_RGBA_5551, 2},
583 {HAL_PIXEL_FORMAT_RGBA_4444, 2},
584 };
585
586 if (gBuf->getPixelFormat() == HAL_PIXEL_FORMAT_YV12) {
587 uint32_t yPlaneOffset, uPlaneOffset, vPlaneOffset;
588 uint32_t yPlaneStride = gBuf->getStride();
589 uint32_t uPlaneStride = ((gBuf->getStride() / 2) + 0xf) & ~0xf;
590 uint32_t vPlaneStride = uPlaneStride;
591 yPlaneOffset = 0;
592 vPlaneOffset = yPlaneOffset + yPlaneStride * gBuf->getHeight();
593 uPlaneOffset = vPlaneOffset
594 + vPlaneStride * (gBuf->getHeight() / 2);
595 *(buf + yPlaneOffset + y * yPlaneStride + x) = pixel & 0xff;
596 *(buf + uPlaneOffset + (y / 2) * uPlaneStride + (x / 2))
597 = (pixel & 0xff00) >> 8;
598 *(buf + vPlaneOffset + (y / 2) * vPlaneStride + (x / 2))
599 = (pixel & 0xff0000) >> 16;
600
601 return;
602 }
603
604 const struct attrib *attrib;
605 for (attrib = attributes; attrib < attributes + NUMA(attributes);
606 attrib++) {
607 if (attrib->format == gBuf->getPixelFormat()) { break; }
608 }
609 if (attrib >= attributes + NUMA(attributes)) {
610 testPrintE("setPixel unsupported format of: %u",
611 gBuf->getPixelFormat());
612 exit(90);
613 }
614
615 memmove(buf + ((gBuf->getStride() * attrib->bytes) * y)
616 + (attrib->bytes * x), &pixel, attrib->bytes);
617}
618
619// Fill a given graphic buffer with a uniform color and alpha
620void hwcTestFillColor(GraphicBuffer *gBuf, ColorFract color, float alpha)
621{
622 unsigned char* buf = NULL;
623 status_t err;
624 uint32_t pixel;
625
626 pixel = hwcTestColor2Pixel(gBuf->getPixelFormat(), color, alpha);
627
628 err = gBuf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&buf));
629 if (err != 0) {
630 testPrintE("hwcTestFillColor lock failed: %d", err);
631 exit(100);
632 }
633
634 for (unsigned int x = 0; x < gBuf->getStride(); x++) {
635 for (unsigned int y = 0; y < gBuf->getHeight(); y++) {
636 uint32_t val = pixel;
637 hwcTestSetPixel(gBuf, buf, x, y, (x < gBuf->getWidth())
638 ? pixel : testRand());
639 }
640 }
641
642 err = gBuf->unlock();
643 if (err != 0) {
644 testPrintE("hwcTestFillColor unlock failed: %d", err);
645 exit(101);
646 }
647}
648
649// Fill the given buffer with a horizontal blend of colors, with the left
650// side color given by startColor and the right side color given by
651// endColor. The startColor and endColor values are specified in the format
652// given by colorFormat, which might be different from the format of the
653// graphic buffer. When different, a color conversion is done when possible
654// to the graphic format of the graphic buffer. A color of black is
655// produced for cases where the conversion is impossible (e.g. out of gamut
656// values).
657void hwcTestFillColorHBlend(GraphicBuffer *gBuf, uint32_t colorFormat,
658 ColorFract startColor, ColorFract endColor)
659{
660 status_t err;
661 unsigned char* buf = NULL;
662 const uint32_t width = gBuf->getWidth();
663 const uint32_t height = gBuf->getHeight();
664 const uint32_t stride = gBuf->getStride();
665
666 err = gBuf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&buf));
667 if (err != 0) {
668 testPrintE("hwcTestFillColorHBlend lock failed: %d", err);
669 exit(110);
670 }
671
672 for (unsigned int x = 0; x < stride; x++) {
673 uint32_t pixel;
674 if (x < width) {
675 ColorFract color(startColor.c1() + (endColor.c1() - startColor.c1())
676 * ((float) x / (float) (width - 1)),
677 startColor.c2() + (endColor.c2() - startColor.c2())
678 * ((float) x / (float) (width - 1)),
679 startColor.c3() + (endColor.c3() - startColor.c3())
680 * ((float) x / (float) (width - 1)));
681
682 // When formats differ, convert colors.
683 // Important to not convert when formats are the same, since
684 // out of gamut colors are always converted to black.
685 if (colorFormat != (uint32_t) gBuf->getPixelFormat()) {
686 hwcTestColorConvert(colorFormat, gBuf->getPixelFormat(), color);
687 }
688 pixel = hwcTestColor2Pixel(gBuf->getPixelFormat(), color, 1.0);
689 } else {
690 // Fill pad with random values
691 pixel = testRand();
692 }
693
694 for (unsigned int y = 0; y <= height; y++) {
695 hwcTestSetPixel(gBuf, buf, x, y, pixel);
696 }
697 }
698
699 err = gBuf->unlock();
700 if (err != 0) {
701 testPrintE("hwcTestFillColorHBlend unlock failed: %d", err);
702 exit(111);
703 }
704}
705
706/*
707 * When possible, converts color specified as a full range value in
708 * the fromFormat, into an equivalent full range color in the toFormat.
709 * When conversion is impossible (e.g. out of gamut color) a color
710 * or black in the full range output format is produced. The input
711 * color is given as a fractional color in the parameter named color.
712 * The produced color is written over the same parameter used to
713 * provide the input color.
714 *
715 * Each graphic format has 3 color components and each of these
716 * components has both a full and in gamut range. This function uses
717 * a table that provides the full and in gamut ranges of each of the
718 * supported graphic formats. The full range is given by members named
719 * c[123]Min to c[123]Max, while the in gamut range is given by members
720 * named c[123]Low to c[123]High. In most cases the full and in gamut
721 * ranges are equivalent. This occurs when the c[123]Min == c[123]Low and
722 * c[123]High == c[123]Max.
723 *
724 * The input and produced colors are both specified as a fractional amount
725 * of the full range. The diagram below provides an overview of the
726 * conversion process. The main steps are:
727 *
728 * 1. Produce black if the input color is out of gamut.
729 *
730 * 2. Convert the in gamut color into the fraction of the fromFromat
731 * in gamut range.
732 *
733 * 3. Convert from the fraction of the in gamut from format range to
734 * the fraction of the in gamut to format range. Produce black
735 * if an equivalent color does not exists.
736 *
737 * 4. Covert from the fraction of the in gamut to format to the
738 * fraction of the full range to format.
739 *
740 * From Format To Format
741 * max high high max
742 * ----+ +-----------+
743 * high \ / \ high
744 * ------\-------------+ +-------->
745 * \
746 * \ +--- black --+
747 * \ / \
748 * \ / +-->
749 * low \ / low
750 * -------- ---+-- black --+
751 * min low low min
752 * ^ ^ ^ ^ ^
753 * | | | | |
754 * | | | | +-- fraction of full range
755 * | | | +-- fraction of valid range
756 * | | +-- fromFormat to toFormat color conversion
757 * | +-- fraction of valid range
758 * +-- fraction of full range
759 */
760void hwcTestColorConvert(uint32_t fromFormat, uint32_t toFormat,
761 ColorFract& color)
762{
763 const struct attrib {
764 uint32_t format;
765 bool rgb;
766 bool yuv;
767 int c1Min, c1Low, c1High, c1Max;
768 int c2Min, c2Low, c2High, c2Max;
769 int c3Min, c3Low, c3High, c3Max;
770 } attributes[] = {
771 {HAL_PIXEL_FORMAT_RGBA_8888, true, false,
772 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255},
773 {HAL_PIXEL_FORMAT_RGBX_8888, true, false,
774 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255},
775 {HAL_PIXEL_FORMAT_RGB_888, true, false,
776 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255},
777 {HAL_PIXEL_FORMAT_RGB_565, true, false,
778 0, 0, 31, 31, 0, 0, 63, 63, 0, 0, 31, 31},
779 {HAL_PIXEL_FORMAT_BGRA_8888, true, false,
780 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255},
781 {HAL_PIXEL_FORMAT_RGBA_5551, true, false,
782 0, 0, 31, 31, 0, 0, 31, 31, 0, 0, 31, 31},
783 {HAL_PIXEL_FORMAT_RGBA_4444, true, false,
784 0, 0, 15, 15, 0, 0, 15, 15, 0, 0, 15, 15},
785 {HAL_PIXEL_FORMAT_YV12, false, true,
786 0, 16, 235, 255, 0, 16, 240, 255, 0, 16, 240, 255},
787 };
788
789 const struct attrib *fromAttrib;
790 for (fromAttrib = attributes; fromAttrib < attributes + NUMA(attributes);
791 fromAttrib++) {
792 if (fromAttrib->format == fromFormat) { break; }
793 }
794 if (fromAttrib >= attributes + NUMA(attributes)) {
795 testPrintE("hwcTestColorConvert unsupported from format of: %u",
796 fromFormat);
797 exit(120);
798 }
799
800 const struct attrib *toAttrib;
801 for (toAttrib = attributes; toAttrib < attributes + NUMA(attributes);
802 toAttrib++) {
803 if (toAttrib->format == toFormat) { break; }
804 }
805 if (toAttrib >= attributes + NUMA(attributes)) {
806 testPrintE("hwcTestColorConvert unsupported to format of: %u",
807 toFormat);
808 exit(121);
809 }
810
811 // Produce black if any of the from components are outside the
812 // valid color range
813 float c1Val = fromAttrib->c1Min
814 + ((float) (fromAttrib->c1Max - fromAttrib->c1Min) * color.c1());
815 float c2Val = fromAttrib->c2Min
816 + ((float) (fromAttrib->c2Max - fromAttrib->c2Min) * color.c2());
817 float c3Val = fromAttrib->c3Min
818 + ((float) (fromAttrib->c3Max - fromAttrib->c3Min) * color.c3());
819 if ((c1Val < fromAttrib->c1Low) || (c1Val > fromAttrib->c1High)
820 || (c2Val < fromAttrib->c2Low) || (c2Val > fromAttrib->c2High)
821 || (c3Val < fromAttrib->c3Low) || (c3Val > fromAttrib->c3High)) {
822
823 // Return black
824 // Will use representation of black from RGBA8888 graphic format
825 // and recursively convert it to the requested graphic format.
826 color = ColorFract(0.0, 0.0, 0.0);
827 hwcTestColorConvert(HAL_PIXEL_FORMAT_RGBA_8888, toFormat, color);
828 return;
829 }
830
831 // Within from format, convert from fraction of full range
832 // to fraction of valid range
833 color = ColorFract((c1Val - fromAttrib->c1Low)
834 / (fromAttrib->c1High - fromAttrib->c1Low),
835 (c2Val - fromAttrib->c2Low)
836 / (fromAttrib->c2High - fromAttrib->c2Low),
837 (c3Val - fromAttrib->c3Low)
838 / (fromAttrib->c3High - fromAttrib->c3Low));
839
840 // If needed perform RGB to YUV conversion
841 float wr = 0.2126, wg = 0.7152, wb = 0.0722; // ITU709 recommended constants
842 if (fromAttrib->rgb && toAttrib->yuv) {
843 float r = color.c1(), g = color.c2(), b = color.c3();
844 float y = wr * r + wg * g + wb * b;
845 float u = 0.5 * ((b - y) / (1.0 - wb)) + 0.5;
846 float v = 0.5 * ((r - y) / (1.0 - wr)) + 0.5;
847
848 // Produce black if color is outside the YUV gamut
849 if ((y < 0.0) || (y > 1.0)
850 || (u < 0.0) || (u > 1.0)
851 || (v < 0.0) || (v > 1.0)) {
852 y = 0.0;
853 u = v = 0.5;
854 }
855
856 color = ColorFract(y, u, v);
857 }
858
859 // If needed perform YUV to RGB conversion
860 // Equations determined from the ITU709 equations for RGB to YUV
861 // conversion, plus the following algebra:
862 //
863 // u = 0.5 * ((b - y) / (1.0 - wb)) + 0.5
864 // 0.5 * ((b - y) / (1.0 - wb)) = u - 0.5
865 // (b - y) / (1.0 - wb) = 2 * (u - 0.5)
866 // b - y = 2 * (u - 0.5) * (1.0 - wb)
867 // b = 2 * (u - 0.5) * (1.0 - wb) + y
868 //
869 // v = 0.5 * ((r -y) / (1.0 - wr)) + 0.5
870 // 0.5 * ((r - y) / (1.0 - wr)) = v - 0.5
871 // (r - y) / (1.0 - wr) = 2 * (v - 0.5)
872 // r - y = 2 * (v - 0.5) * (1.0 - wr)
873 // r = 2 * (v - 0.5) * (1.0 - wr) + y
874 //
875 // y = wr * r + wg * g + wb * b
876 // wr * r + wg * g + wb * b = y
877 // wg * g = y - wr * r - wb * b
878 // g = (y - wr * r - wb * b) / wg
879 if (fromAttrib->yuv && toAttrib->rgb) {
880 float y = color.c1(), u = color.c2(), v = color.c3();
881 float r = 2.0 * (v - 0.5) * (1.0 - wr) + y;
882 float b = 2.0 * (u - 0.5) * (1.0 - wb) + y;
883 float g = (y - wr * r - wb * b) / wg;
884
885 // Produce black if color is outside the RGB gamut
886 if ((r < 0.0) || (r > 1.0)
887 || (g < 0.0) || (g > 1.0)
888 || (b < 0.0) || (b > 1.0)) {
889 r = g = b = 0.0;
890 }
891
892 color = ColorFract(r, g, b);
893 }
894
895 // Within to format, convert from fraction of valid range
896 // to fraction of full range
897 c1Val = (toAttrib->c1Low
898 + (float) (toAttrib->c1High - toAttrib->c1Low) * color.c1());
899 c2Val = (toAttrib->c1Low
900 + (float) (toAttrib->c2High - toAttrib->c2Low) * color.c2());
901 c3Val = (toAttrib->c1Low
902 + (float) (toAttrib->c3High - toAttrib->c3Low) * color.c3());
903 color = ColorFract((float) (c1Val - toAttrib->c1Min)
904 / (float) (toAttrib->c1Max - toAttrib->c1Min),
905 (float) (c2Val - toAttrib->c2Min)
906 / (float) (toAttrib->c2Max - toAttrib->c2Min),
907 (float) (c3Val - toAttrib->c3Min)
908 / (float) (toAttrib->c3Max - toAttrib->c3Min));
909}
910
911// TODO: Use PrintGLString, CechckGlError, and PrintEGLConfiguration
912// from libglTest
913static void printGLString(const char *name, GLenum s)
914{
915 const char *v = (const char *) glGetString(s);
916
917 if (v == NULL) {
918 testPrintI("GL %s unknown", name);
919 } else {
920 testPrintI("GL %s = %s", name, v);
921 }
922}
923
924static void checkEglError(const char* op, EGLBoolean returnVal)
925{
926 if (returnVal != EGL_TRUE) {
927 testPrintE("%s() returned %d", op, returnVal);
928 }
929
930 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
931 = eglGetError()) {
932 testPrintE("after %s() eglError %s (0x%x)",
933 op, EGLUtils::strerror(error), error);
934 }
935}
936
937static void checkGlError(const char* op)
938{
939 for (GLint error = glGetError(); error; error
940 = glGetError()) {
941 testPrintE("after %s() glError (0x%x)", op, error);
942 }
943}
944
945static void printEGLConfiguration(EGLDisplay dpy, EGLConfig config)
946{
947
948#define X(VAL) {VAL, #VAL}
949 struct {EGLint attribute; const char* name;} names[] = {
950 X(EGL_BUFFER_SIZE),
951 X(EGL_ALPHA_SIZE),
952 X(EGL_BLUE_SIZE),
953 X(EGL_GREEN_SIZE),
954 X(EGL_RED_SIZE),
955 X(EGL_DEPTH_SIZE),
956 X(EGL_STENCIL_SIZE),
957 X(EGL_CONFIG_CAVEAT),
958 X(EGL_CONFIG_ID),
959 X(EGL_LEVEL),
960 X(EGL_MAX_PBUFFER_HEIGHT),
961 X(EGL_MAX_PBUFFER_PIXELS),
962 X(EGL_MAX_PBUFFER_WIDTH),
963 X(EGL_NATIVE_RENDERABLE),
964 X(EGL_NATIVE_VISUAL_ID),
965 X(EGL_NATIVE_VISUAL_TYPE),
966 X(EGL_SAMPLES),
967 X(EGL_SAMPLE_BUFFERS),
968 X(EGL_SURFACE_TYPE),
969 X(EGL_TRANSPARENT_TYPE),
970 X(EGL_TRANSPARENT_RED_VALUE),
971 X(EGL_TRANSPARENT_GREEN_VALUE),
972 X(EGL_TRANSPARENT_BLUE_VALUE),
973 X(EGL_BIND_TO_TEXTURE_RGB),
974 X(EGL_BIND_TO_TEXTURE_RGBA),
975 X(EGL_MIN_SWAP_INTERVAL),
976 X(EGL_MAX_SWAP_INTERVAL),
977 X(EGL_LUMINANCE_SIZE),
978 X(EGL_ALPHA_MASK_SIZE),
979 X(EGL_COLOR_BUFFER_TYPE),
980 X(EGL_RENDERABLE_TYPE),
981 X(EGL_CONFORMANT),
982 };
983#undef X
984
985 for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
986 EGLint value = -1;
987 EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute,
988 &value);
989 EGLint error = eglGetError();
990 if (returnVal && error == EGL_SUCCESS) {
991 testPrintI(" %s: %d (%#x)", names[j].name, value, value);
992 }
993 }
994 testPrintI("");
995}