SRGB_test.cpp revision b8072d84ba9bf43701fcace61414b3cef5910919
1/* 2 * Copyright 2013 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#define LOG_TAG "SRGB_test" 18//#define LOG_NDEBUG 0 19 20#include "GLTest.h" 21 22#include <gui/CpuConsumer.h> 23#include <gui/Surface.h> 24#include <gui/SurfaceComposerClient.h> 25 26#include <EGL/egl.h> 27#include <EGL/eglext.h> 28#include <GLES3/gl3.h> 29 30#include <android/native_window.h> 31 32#include <gtest/gtest.h> 33 34namespace android { 35 36class SRGBTest : public ::testing::Test { 37protected: 38 // Class constants 39 enum { 40 DISPLAY_WIDTH = 512, 41 DISPLAY_HEIGHT = 512, 42 PIXEL_SIZE = 4, // bytes or components 43 DISPLAY_SIZE = DISPLAY_WIDTH * DISPLAY_HEIGHT * PIXEL_SIZE, 44 ALPHA_VALUE = 223, // should be in [0, 255] 45 TOLERANCE = 1, 46 }; 47 static const char SHOW_DEBUG_STRING[]; 48 49 SRGBTest() : 50 mInputSurface(), mCpuConsumer(), mLockedBuffer(), 51 mEglDisplay(EGL_NO_DISPLAY), mEglConfig(), 52 mEglContext(EGL_NO_CONTEXT), mEglSurface(EGL_NO_SURFACE), 53 mComposerClient(), mSurfaceControl(), mOutputSurface() { 54 } 55 56 virtual ~SRGBTest() { 57 if (mEglDisplay != EGL_NO_DISPLAY) { 58 if (mEglSurface != EGL_NO_SURFACE) { 59 eglDestroySurface(mEglDisplay, mEglSurface); 60 } 61 if (mEglContext != EGL_NO_CONTEXT) { 62 eglDestroyContext(mEglDisplay, mEglContext); 63 } 64 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 65 EGL_NO_CONTEXT); 66 eglTerminate(mEglDisplay); 67 } 68 } 69 70 virtual void SetUp() { 71 mBufferQueue = new BufferQueue(); 72 ASSERT_EQ(NO_ERROR, mBufferQueue->setDefaultBufferSize( 73 DISPLAY_WIDTH, DISPLAY_HEIGHT)); 74 mCpuConsumer = new CpuConsumer(mBufferQueue, 1); 75 String8 name("CpuConsumer_for_SRGBTest"); 76 mCpuConsumer->setName(name); 77 mInputSurface = new Surface(mBufferQueue); 78 79 ASSERT_NO_FATAL_FAILURE(createEGLSurface(mInputSurface.get())); 80 ASSERT_NO_FATAL_FAILURE(createDebugSurface()); 81 } 82 83 virtual void TearDown() { 84 ASSERT_NO_FATAL_FAILURE(copyToDebugSurface()); 85 ASSERT_TRUE(mLockedBuffer.data != NULL); 86 ASSERT_EQ(NO_ERROR, mCpuConsumer->unlockBuffer(mLockedBuffer)); 87 } 88 89 static float linearToSRGB(float l) { 90 if (l <= 0.0031308f) { 91 return l * 12.92f; 92 } else { 93 return 1.055f * pow(l, (1 / 2.4f)) - 0.055f; 94 } 95 } 96 97 static float srgbToLinear(float s) { 98 if (s <= 0.04045) { 99 return s / 12.92f; 100 } else { 101 return pow(((s + 0.055f) / 1.055f), 2.4f); 102 } 103 } 104 105 static uint8_t srgbToLinear(uint8_t u) { 106 float f = u / 255.0f; 107 return static_cast<uint8_t>(srgbToLinear(f) * 255.0f + 0.5f); 108 } 109 110 void fillTexture(bool writeAsSRGB) { 111 uint8_t* textureData = new uint8_t[DISPLAY_SIZE]; 112 113 for (int y = 0; y < DISPLAY_HEIGHT; ++y) { 114 for (int x = 0; x < DISPLAY_WIDTH; ++x) { 115 float realValue = static_cast<float>(x) / (DISPLAY_WIDTH - 1); 116 realValue *= ALPHA_VALUE / 255.0f; // Premultiply by alpha 117 if (writeAsSRGB) { 118 realValue = linearToSRGB(realValue); 119 } 120 121 int offset = (y * DISPLAY_WIDTH + x) * PIXEL_SIZE; 122 for (int c = 0; c < 3; ++c) { 123 uint8_t intValue = static_cast<uint8_t>( 124 realValue * 255.0f + 0.5f); 125 textureData[offset + c] = intValue; 126 } 127 textureData[offset + 3] = ALPHA_VALUE; 128 } 129 } 130 131 glTexImage2D(GL_TEXTURE_2D, 0, writeAsSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA8, 132 DISPLAY_WIDTH, DISPLAY_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 133 textureData); 134 ASSERT_EQ(GL_NO_ERROR, glGetError()); 135 136 delete[] textureData; 137 } 138 139 void initShaders() { 140 static const char vertexSource[] = 141 "attribute vec4 vPosition;\n" 142 "varying vec2 texCoords;\n" 143 "void main() {\n" 144 " texCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n" 145 " gl_Position = vPosition;\n" 146 "}\n"; 147 148 static const char fragmentSource[] = 149 "precision mediump float;\n" 150 "uniform sampler2D texSampler;\n" 151 "varying vec2 texCoords;\n" 152 "void main() {\n" 153 " gl_FragColor = texture2D(texSampler, texCoords);\n" 154 "}\n"; 155 156 GLuint program; 157 { 158 SCOPED_TRACE("Creating shader program"); 159 ASSERT_NO_FATAL_FAILURE(GLTest::createProgram( 160 vertexSource, fragmentSource, &program)); 161 } 162 163 GLint positionHandle = glGetAttribLocation(program, "vPosition"); 164 ASSERT_EQ(GL_NO_ERROR, glGetError()); 165 ASSERT_NE(-1, positionHandle); 166 167 GLint samplerHandle = glGetUniformLocation(program, "texSampler"); 168 ASSERT_EQ(GL_NO_ERROR, glGetError()); 169 ASSERT_NE(-1, samplerHandle); 170 171 static const GLfloat vertices[] = { 172 -1.0f, 1.0f, 173 -1.0f, -1.0f, 174 1.0f, -1.0f, 175 1.0f, 1.0f, 176 }; 177 178 glVertexAttribPointer(positionHandle, 2, GL_FLOAT, GL_FALSE, 0, vertices); 179 ASSERT_EQ(GL_NO_ERROR, glGetError()); 180 glEnableVertexAttribArray(positionHandle); 181 ASSERT_EQ(GL_NO_ERROR, glGetError()); 182 183 glUseProgram(program); 184 ASSERT_EQ(GL_NO_ERROR, glGetError()); 185 glUniform1i(samplerHandle, 0); 186 ASSERT_EQ(GL_NO_ERROR, glGetError()); 187 188 GLuint textureHandle; 189 glGenTextures(1, &textureHandle); 190 ASSERT_EQ(GL_NO_ERROR, glGetError()); 191 glBindTexture(GL_TEXTURE_2D, textureHandle); 192 ASSERT_EQ(GL_NO_ERROR, glGetError()); 193 194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 195 ASSERT_EQ(GL_NO_ERROR, glGetError()); 196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 197 ASSERT_EQ(GL_NO_ERROR, glGetError()); 198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 199 ASSERT_EQ(GL_NO_ERROR, glGetError()); 200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 201 ASSERT_EQ(GL_NO_ERROR, glGetError()); 202 } 203 204 void drawTexture(bool asSRGB, GLint x, GLint y, GLsizei width, 205 GLsizei height) { 206 ASSERT_NO_FATAL_FAILURE(fillTexture(asSRGB)); 207 glViewport(x, y, width, height); 208 ASSERT_EQ(GL_NO_ERROR, glGetError()); 209 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 210 ASSERT_EQ(GL_NO_ERROR, glGetError()); 211 } 212 213 void checkLockedBuffer(PixelFormat format) { 214 ASSERT_EQ(mLockedBuffer.format, format); 215 ASSERT_EQ(mLockedBuffer.width, DISPLAY_WIDTH); 216 ASSERT_EQ(mLockedBuffer.height, DISPLAY_HEIGHT); 217 } 218 219 static bool withinTolerance(int a, int b) { 220 int diff = a - b; 221 return diff >= 0 ? diff <= TOLERANCE : -diff <= TOLERANCE; 222 } 223 224 // Primary producer and consumer 225 sp<BufferQueue> mBufferQueue; 226 sp<Surface> mInputSurface; 227 sp<CpuConsumer> mCpuConsumer; 228 CpuConsumer::LockedBuffer mLockedBuffer; 229 230 EGLDisplay mEglDisplay; 231 EGLConfig mEglConfig; 232 EGLContext mEglContext; 233 EGLSurface mEglSurface; 234 235 // Auxiliary display output 236 sp<SurfaceComposerClient> mComposerClient; 237 sp<SurfaceControl> mSurfaceControl; 238 sp<Surface> mOutputSurface; 239 240private: 241 void createEGLSurface(Surface* inputSurface) { 242 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 243 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 244 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay); 245 246 EXPECT_TRUE(eglInitialize(mEglDisplay, NULL, NULL)); 247 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 248 249 static const EGLint configAttribs[] = { 250 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 251 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, 252 EGL_RED_SIZE, 8, 253 EGL_GREEN_SIZE, 8, 254 EGL_BLUE_SIZE, 8, 255 EGL_ALPHA_SIZE, 8, 256 EGL_NONE }; 257 258 EGLint numConfigs = 0; 259 EXPECT_TRUE(eglChooseConfig(mEglDisplay, configAttribs, &mEglConfig, 1, 260 &numConfigs)); 261 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 262 ASSERT_GT(numConfigs, 0); 263 264 static const EGLint contextAttribs[] = { 265 EGL_CONTEXT_CLIENT_VERSION, 3, 266 EGL_NONE } ; 267 268 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, 269 contextAttribs); 270 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 271 ASSERT_NE(EGL_NO_CONTEXT, mEglContext); 272 273 mEglSurface = eglCreateWindowSurface(mEglDisplay, mEglConfig, 274 inputSurface, NULL); 275 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 276 ASSERT_NE(EGL_NO_SURFACE, mEglSurface); 277 278 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 279 mEglContext)); 280 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 281 } 282 283 void createDebugSurface() { 284 if (getenv(SHOW_DEBUG_STRING) == NULL) return; 285 286 mComposerClient = new SurfaceComposerClient; 287 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); 288 289 mSurfaceControl = mComposerClient->createSurface( 290 String8("SRGBTest Surface"), DISPLAY_WIDTH, DISPLAY_HEIGHT, 291 PIXEL_FORMAT_RGBA_8888); 292 293 ASSERT_TRUE(mSurfaceControl != NULL); 294 ASSERT_TRUE(mSurfaceControl->isValid()); 295 296 SurfaceComposerClient::openGlobalTransaction(); 297 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF)); 298 ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); 299 SurfaceComposerClient::closeGlobalTransaction(); 300 301 ANativeWindow_Buffer outBuffer; 302 ARect inOutDirtyBounds; 303 mOutputSurface = mSurfaceControl->getSurface(); 304 mOutputSurface->lock(&outBuffer, &inOutDirtyBounds); 305 uint8_t* bytePointer = reinterpret_cast<uint8_t*>(outBuffer.bits); 306 for (int y = 0; y < outBuffer.height; ++y) { 307 int rowOffset = y * outBuffer.stride; // pixels 308 for (int x = 0; x < outBuffer.width; ++x) { 309 int colOffset = (rowOffset + x) * PIXEL_SIZE; // bytes 310 for (int c = 0; c < PIXEL_SIZE; ++c) { 311 int offset = colOffset + c; 312 bytePointer[offset] = ((c + 1) * 56) - 1; 313 } 314 } 315 } 316 mOutputSurface->unlockAndPost(); 317 } 318 319 void copyToDebugSurface() { 320 if (!mOutputSurface.get()) return; 321 322 size_t bufferSize = mLockedBuffer.height * mLockedBuffer.stride * 323 PIXEL_SIZE; 324 325 ANativeWindow_Buffer outBuffer; 326 ARect outBufferBounds; 327 mOutputSurface->lock(&outBuffer, &outBufferBounds); 328 ASSERT_EQ(mLockedBuffer.width, outBuffer.width); 329 ASSERT_EQ(mLockedBuffer.height, outBuffer.height); 330 ASSERT_EQ(mLockedBuffer.stride, outBuffer.stride); 331 332 if (mLockedBuffer.format == outBuffer.format) { 333 memcpy(outBuffer.bits, mLockedBuffer.data, bufferSize); 334 } else { 335 ASSERT_EQ(mLockedBuffer.format, PIXEL_FORMAT_sRGB_A_8888); 336 ASSERT_EQ(outBuffer.format, PIXEL_FORMAT_RGBA_8888); 337 uint8_t* outPointer = reinterpret_cast<uint8_t*>(outBuffer.bits); 338 for (int y = 0; y < outBuffer.height; ++y) { 339 int rowOffset = y * outBuffer.stride; // pixels 340 for (int x = 0; x < outBuffer.width; ++x) { 341 int colOffset = (rowOffset + x) * PIXEL_SIZE; // bytes 342 343 // RGB are converted 344 for (int c = 0; c < (PIXEL_SIZE - 1); ++c) { 345 outPointer[colOffset + c] = srgbToLinear( 346 mLockedBuffer.data[colOffset + c]); 347 } 348 349 // Alpha isn't converted 350 outPointer[colOffset + 3] = 351 mLockedBuffer.data[colOffset + 3]; 352 } 353 } 354 } 355 mOutputSurface->unlockAndPost(); 356 357 int sleepSeconds = atoi(getenv(SHOW_DEBUG_STRING)); 358 sleep(sleepSeconds); 359 } 360}; 361 362const char SRGBTest::SHOW_DEBUG_STRING[] = "DEBUG_OUTPUT_SECONDS"; 363 364TEST_F(SRGBTest, GLRenderFromSRGBTexture) { 365 ASSERT_NO_FATAL_FAILURE(initShaders()); 366 367 // The RGB texture is displayed in the top half 368 ASSERT_NO_FATAL_FAILURE(drawTexture(false, 0, DISPLAY_HEIGHT / 2, 369 DISPLAY_WIDTH, DISPLAY_HEIGHT / 2)); 370 371 // The SRGB texture is displayed in the bottom half 372 ASSERT_NO_FATAL_FAILURE(drawTexture(true, 0, 0, 373 DISPLAY_WIDTH, DISPLAY_HEIGHT / 2)); 374 375 eglSwapBuffers(mEglDisplay, mEglSurface); 376 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 377 378 // Lock 379 ASSERT_EQ(NO_ERROR, mCpuConsumer->lockNextBuffer(&mLockedBuffer)); 380 ASSERT_NO_FATAL_FAILURE(checkLockedBuffer(PIXEL_FORMAT_RGBA_8888)); 381 382 // Compare a pixel in the middle of each texture 383 int midSRGBOffset = (DISPLAY_HEIGHT / 4) * mLockedBuffer.stride * 384 PIXEL_SIZE; 385 int midRGBOffset = midSRGBOffset * 3; 386 midRGBOffset += (DISPLAY_WIDTH / 2) * PIXEL_SIZE; 387 midSRGBOffset += (DISPLAY_WIDTH / 2) * PIXEL_SIZE; 388 for (int c = 0; c < PIXEL_SIZE; ++c) { 389 int expectedValue = mLockedBuffer.data[midRGBOffset + c]; 390 int actualValue = mLockedBuffer.data[midSRGBOffset + c]; 391 ASSERT_PRED2(withinTolerance, expectedValue, actualValue); 392 } 393 394 // mLockedBuffer is unlocked in TearDown so we can copy data from it to 395 // the debug surface if necessary 396} 397 398TEST_F(SRGBTest, RenderToSRGBSurface) { 399 ASSERT_NO_FATAL_FAILURE(initShaders()); 400 401 // By default, the first buffer we write into will be RGB 402 403 // Render an RGB texture across the whole surface 404 ASSERT_NO_FATAL_FAILURE(drawTexture(false, 0, 0, 405 DISPLAY_WIDTH, DISPLAY_HEIGHT)); 406 eglSwapBuffers(mEglDisplay, mEglSurface); 407 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 408 409 // Lock 410 ASSERT_EQ(NO_ERROR, mCpuConsumer->lockNextBuffer(&mLockedBuffer)); 411 ASSERT_NO_FATAL_FAILURE(checkLockedBuffer(PIXEL_FORMAT_RGBA_8888)); 412 413 // Save the values of the middle pixel for later comparison against SRGB 414 uint8_t values[PIXEL_SIZE] = {}; 415 int middleOffset = (DISPLAY_HEIGHT / 2) * mLockedBuffer.stride * 416 PIXEL_SIZE; 417 middleOffset += (DISPLAY_WIDTH / 2) * PIXEL_SIZE; 418 for (int c = 0; c < PIXEL_SIZE; ++c) { 419 values[c] = mLockedBuffer.data[middleOffset + c]; 420 } 421 422 // Unlock 423 ASSERT_EQ(NO_ERROR, mCpuConsumer->unlockBuffer(mLockedBuffer)); 424 425 // Switch to SRGB window surface 426#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE 427#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB 428 429 static const int srgbAttribs[] = { 430 EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR, 431 EGL_NONE, 432 }; 433 434 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 435 mEglContext)); 436 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 437 438 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mEglSurface)); 439 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 440 441 mEglSurface = eglCreateWindowSurface(mEglDisplay, mEglConfig, 442 mInputSurface.get(), srgbAttribs); 443 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 444 ASSERT_NE(EGL_NO_SURFACE, mEglSurface); 445 446 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 447 mEglContext)); 448 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 449 450 // Render the texture again 451 ASSERT_NO_FATAL_FAILURE(drawTexture(false, 0, 0, 452 DISPLAY_WIDTH, DISPLAY_HEIGHT)); 453 eglSwapBuffers(mEglDisplay, mEglSurface); 454 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 455 456 // Lock 457 ASSERT_EQ(NO_ERROR, mCpuConsumer->lockNextBuffer(&mLockedBuffer)); 458 459 // Make sure we actually got the SRGB buffer on the consumer side 460 ASSERT_NO_FATAL_FAILURE(checkLockedBuffer(PIXEL_FORMAT_sRGB_A_8888)); 461 462 // Verify that the stored value is the same, accounting for RGB/SRGB 463 for (int c = 0; c < PIXEL_SIZE; ++c) { 464 // The alpha value should be equivalent before linear->SRGB 465 float rgbAsSRGB = (c == 3) ? values[c] / 255.0f : 466 linearToSRGB(values[c] / 255.0f); 467 int expectedValue = rgbAsSRGB * 255.0f + 0.5f; 468 int actualValue = mLockedBuffer.data[middleOffset + c]; 469 ASSERT_PRED2(withinTolerance, expectedValue, actualValue); 470 } 471 472 // mLockedBuffer is unlocked in TearDown so we can copy data from it to 473 // the debug surface if necessary 474} 475 476} // namespace android 477