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