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