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