1 /* 2 * Copyright (C) 2017 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 #include <sstream> 18 #include <android/hardware/graphics/common/1.0/types.h> 19 20 #include "Hwc2TestPixelComparator.h" 21 22 using android::hardware::graphics::common::V1_0::BufferUsage; 23 24 uint32_t ComparatorResult::getPixel(int32_t x, int32_t y, uint32_t stride, 25 uint8_t* img) const 26 { 27 uint32_t r = img[(y * stride + x) * 4 + 0]; 28 uint32_t g = img[(y * stride + x) * 4 + 1]; 29 uint32_t b = img[(y * stride + x) * 4 + 2]; 30 uint32_t a = img[(y * stride + x) * 4 + 3]; 31 32 uint32_t pixel = 0; 33 pixel |= r; 34 pixel |= g << 8; 35 pixel |= b << 16; 36 pixel |= a << 24; 37 return pixel; 38 } 39 40 void ComparatorResult::CompareBuffers( 41 android::sp<android::GraphicBuffer>& resultBuffer, 42 android::sp<android::GraphicBuffer>& expectedBuffer) 43 { 44 uint8_t* resultBufferImg; 45 uint8_t* expectedBufferImg; 46 resultBuffer->lock(static_cast<uint32_t>(BufferUsage::CPU_READ_OFTEN), 47 (void**)(&resultBufferImg)); 48 49 expectedBuffer->lock(static_cast<uint32_t>(BufferUsage::CPU_READ_OFTEN), 50 (void**)(&expectedBufferImg)); 51 mComparisons.clear(); 52 int32_t mDifferentPixelCount = 0; 53 int32_t mBlankPixelCount = 0; 54 55 for (uint32_t y = 0; y < resultBuffer->getHeight(); y++) { 56 for (uint32_t x = 0; x < resultBuffer->getWidth(); x++) { 57 uint32_t result = getPixel(x, y, resultBuffer->getStride(), 58 resultBufferImg); 59 uint32_t expected = getPixel(x, y, expectedBuffer->getStride(), 60 expectedBufferImg); 61 62 if (result == 0) 63 mBlankPixelCount++; 64 65 if (result != expected) 66 mDifferentPixelCount++; 67 68 mComparisons.emplace_back(std::make_tuple(x, y, result, expected)); 69 } 70 } 71 resultBuffer->unlock(); 72 expectedBuffer->unlock(); 73 } 74 75 std::string ComparatorResult::pixelDiff(uint32_t x, uint32_t y, 76 uint32_t resultPixel, uint32_t expectedPixel) const 77 { 78 uint32_t resultAlpha = (resultPixel >> 24) & 0xFF; 79 uint32_t resultBlue = (resultPixel >> 16) & 0xFF; 80 uint32_t resultGreen = (resultPixel >> 8) & 0xFF; 81 uint32_t resultRed = resultPixel & 0xFF; 82 83 uint32_t expectedAlpha = (expectedPixel >> 24) & 0xFF; 84 uint32_t expectedBlue = (expectedPixel >> 16) & 0xFF; 85 uint32_t expectedGreen = (expectedPixel >> 8) & 0xFF; 86 uint32_t expectedRed = expectedPixel & 0xFF; 87 88 std::ostringstream stream; 89 90 stream << "x: " << x << " y: " << y << std::endl; 91 stream << std::hex; 92 stream << "Result pixel: " << resultRed << "|" << resultGreen << "|" 93 << resultBlue << "|" << resultAlpha << std::endl; 94 95 stream << "Expected pixel: " << expectedRed << "|" << expectedGreen << "|" 96 << expectedBlue << "|" << expectedAlpha << std::endl; 97 98 return stream.str(); 99 } 100 101 std::string ComparatorResult::dumpComparison() const 102 { 103 std::ostringstream stream; 104 stream << "Number of different pixels: " << mDifferentPixelCount; 105 106 for (const auto& comparison : mComparisons) { 107 if (std::get<2>(comparison) != std::get<3>(comparison)) 108 stream << pixelDiff(std::get<0>(comparison), 109 std::get<1>(comparison), std::get<2>(comparison), 110 std::get<3>(comparison)); 111 } 112 return stream.str(); 113 } 114