Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [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 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "RenderEngine" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 22 | #include "GLES20RenderEngine.h" |
| 23 | |
| 24 | #include <math.h> |
| 25 | #include <fstream> |
| 26 | #include <sstream> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 27 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 28 | #include <GLES2/gl2.h> |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 29 | #include <GLES2/gl2ext.h> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 30 | #include <cutils/compiler.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 31 | #include <renderengine/Mesh.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 32 | #include <renderengine/Texture.h> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 33 | #include <renderengine/private/Description.h> |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 34 | #include <ui/ColorSpace.h> |
| 35 | #include <ui/DebugUtils.h> |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 36 | #include <ui/Rect.h> |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 37 | #include <ui/Region.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 38 | #include <utils/String8.h> |
| 39 | #include <utils/Trace.h> |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 40 | #include "GLExtensions.h" |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 41 | #include "GLFramebuffer.h" |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 42 | #include "GLImage.h" |
| 43 | #include "GLSurface.h" |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 44 | #include "Program.h" |
| 45 | #include "ProgramCache.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 46 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 47 | bool checkGlError(const char* op, int lineNumber) { |
| 48 | bool errorFound = false; |
| 49 | GLint error = glGetError(); |
| 50 | while (error != GL_NO_ERROR) { |
| 51 | errorFound = true; |
| 52 | error = glGetError(); |
| 53 | ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error); |
| 54 | } |
| 55 | return errorFound; |
| 56 | } |
| 57 | |
Courtney Goeltzenleuchter | 4f20f9c | 2017-04-06 08:18:34 -0600 | [diff] [blame] | 58 | static constexpr bool outputDebugPPMs = false; |
| 59 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 60 | void writePPM(const char* basename, GLuint width, GLuint height) { |
| 61 | ALOGV("writePPM #%s: %d x %d", basename, width, height); |
| 62 | |
| 63 | std::vector<GLubyte> pixels(width * height * 4); |
| 64 | std::vector<GLubyte> outBuffer(width * height * 3); |
| 65 | |
| 66 | // TODO(courtneygo): We can now have float formats, need |
| 67 | // to remove this code or update to support. |
| 68 | // Make returned pixels fit in uint32_t, one byte per component |
| 69 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data()); |
| 70 | if (checkGlError(__FUNCTION__, __LINE__)) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | std::string filename(basename); |
| 75 | filename.append(".ppm"); |
| 76 | std::ofstream file(filename.c_str(), std::ios::binary); |
| 77 | if (!file.is_open()) { |
| 78 | ALOGE("Unable to open file: %s", filename.c_str()); |
| 79 | ALOGE("You may need to do: \"adb shell setenforce 0\" to enable " |
| 80 | "surfaceflinger to write debug images"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | file << "P6\n"; |
| 85 | file << width << "\n"; |
| 86 | file << height << "\n"; |
| 87 | file << 255 << "\n"; |
| 88 | |
| 89 | auto ptr = reinterpret_cast<char*>(pixels.data()); |
| 90 | auto outPtr = reinterpret_cast<char*>(outBuffer.data()); |
| 91 | for (int y = height - 1; y >= 0; y--) { |
| 92 | char* data = ptr + y * width * sizeof(uint32_t); |
| 93 | |
| 94 | for (GLuint x = 0; x < width; x++) { |
| 95 | // Only copy R, G and B components |
| 96 | outPtr[0] = data[0]; |
| 97 | outPtr[1] = data[1]; |
| 98 | outPtr[2] = data[2]; |
| 99 | data += sizeof(uint32_t); |
| 100 | outPtr += 3; |
| 101 | } |
| 102 | } |
| 103 | file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size()); |
| 104 | } |
| 105 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 106 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 107 | namespace renderengine { |
| 108 | namespace gl { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 109 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 110 | using ui::Dataspace; |
| 111 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 112 | GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags) |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 113 | : RenderEngine(featureFlags), |
| 114 | mVpWidth(0), |
| 115 | mVpHeight(0), |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 116 | mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 117 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 118 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 119 | |
| 120 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 121 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 122 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 123 | const uint16_t protTexData[] = {0}; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 124 | glGenTextures(1, &mProtectedTexName); |
| 125 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 126 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 127 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 128 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 130 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 131 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 132 | // mColorBlindnessCorrection = M; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 133 | |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 134 | if (mUseColorManagement) { |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 135 | ColorSpace srgb(ColorSpace::sRGB()); |
| 136 | ColorSpace displayP3(ColorSpace::DisplayP3()); |
| 137 | ColorSpace bt2020(ColorSpace::BT2020()); |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 138 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 139 | // Compute sRGB to Display P3 transform matrix. |
| 140 | // NOTE: For now, we are limiting output wide color space support to |
| 141 | // Display-P3 only. |
| 142 | mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform()); |
| 143 | |
| 144 | // Compute Display P3 to sRGB transform matrix. |
| 145 | mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform()); |
| 146 | |
| 147 | // no chromatic adaptation needed since all color spaces use D65 for their white points. |
| 148 | mSrgbToXyz = srgb.getRGBtoXYZ(); |
| 149 | mDisplayP3ToXyz = displayP3.getRGBtoXYZ(); |
| 150 | mBt2020ToXyz = bt2020.getRGBtoXYZ(); |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 151 | mXyzToSrgb = mat4(srgb.getXYZtoRGB()); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 152 | mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB()); |
| 153 | mXyzToBt2020 = mat4(bt2020.getXYZtoRGB()); |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 154 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 157 | GLES20RenderEngine::~GLES20RenderEngine() {} |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 158 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 159 | std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() { |
| 160 | return std::make_unique<GLFramebuffer>(*this); |
| 161 | } |
| 162 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 163 | std::unique_ptr<Surface> GLES20RenderEngine::createSurface() { |
| 164 | return std::make_unique<GLSurface>(*this); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 167 | std::unique_ptr<Image> GLES20RenderEngine::createImage() { |
| 168 | return std::make_unique<GLImage>(*this); |
| 169 | } |
| 170 | |
| 171 | void GLES20RenderEngine::primeCache() const { |
| 172 | ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT); |
| 173 | } |
| 174 | |
| 175 | bool GLES20RenderEngine::isCurrent() const { |
| 176 | return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext(); |
| 177 | } |
| 178 | |
| 179 | bool GLES20RenderEngine::setCurrentSurface(const Surface& surface) { |
| 180 | // Surface is an abstract interface. GLES20RenderEngine only ever |
| 181 | // creates GLSurface's, so it is safe to just cast to the actual |
| 182 | // type. |
| 183 | bool success = true; |
| 184 | const GLSurface& glSurface = static_cast<const GLSurface&>(surface); |
| 185 | EGLSurface eglSurface = glSurface.getEGLSurface(); |
| 186 | if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) { |
| 187 | success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE; |
| 188 | if (success && glSurface.getAsync()) { |
| 189 | eglSwapInterval(mEGLDisplay, 0); |
| 190 | } |
| 191 | } |
| 192 | return success; |
| 193 | } |
| 194 | |
| 195 | void GLES20RenderEngine::resetCurrentSurface() { |
| 196 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 197 | } |
| 198 | |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 199 | base::unique_fd GLES20RenderEngine::flush() { |
| 200 | if (!GLExtensions::getInstance().hasNativeFenceSync()) { |
| 201 | return base::unique_fd(); |
| 202 | } |
| 203 | |
| 204 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
| 205 | if (sync == EGL_NO_SYNC_KHR) { |
| 206 | ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); |
| 207 | return base::unique_fd(); |
| 208 | } |
| 209 | |
| 210 | // native fence fd will not be populated until flush() is done. |
| 211 | glFlush(); |
| 212 | |
| 213 | // get the fence fd |
| 214 | base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); |
| 215 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 216 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 217 | ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); |
| 218 | } |
| 219 | |
| 220 | return fenceFd; |
| 221 | } |
| 222 | |
| 223 | bool GLES20RenderEngine::finish() { |
| 224 | if (!GLExtensions::getInstance().hasFenceSync()) { |
| 225 | ALOGW("no synchronization support"); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr); |
| 230 | if (sync == EGL_NO_SYNC_KHR) { |
| 231 | ALOGW("failed to create EGL fence sync: %#x", eglGetError()); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, |
| 236 | 2000000000 /*2 sec*/); |
| 237 | EGLint error = eglGetError(); |
| 238 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 239 | if (result != EGL_CONDITION_SATISFIED_KHR) { |
| 240 | if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 241 | ALOGW("fence wait timed out"); |
| 242 | } else { |
| 243 | ALOGW("error waiting on EGL fence: %#x", error); |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) { |
| 252 | if (!GLExtensions::getInstance().hasNativeFenceSync() || |
| 253 | !GLExtensions::getInstance().hasWaitSync()) { |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| 258 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 259 | if (sync == EGL_NO_SYNC_KHR) { |
| 260 | ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | // fenceFd is now owned by EGLSync |
| 265 | (void)fenceFd.release(); |
| 266 | |
| 267 | // XXX: The spec draft is inconsistent as to whether this should return an |
| 268 | // EGLint or void. Ignore the return value for now, as it's not strictly |
| 269 | // needed. |
| 270 | eglWaitSyncKHR(mEGLDisplay, sync, 0); |
| 271 | EGLint error = eglGetError(); |
| 272 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 273 | if (error != EGL_SUCCESS) { |
| 274 | ALOGE("failed to wait for EGL native fence sync: %#x", error); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | void GLES20RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red, |
| 282 | float green, float blue, float alpha) { |
| 283 | size_t c; |
| 284 | Rect const* r = region.getArray(&c); |
| 285 | Mesh mesh(Mesh::TRIANGLES, c * 6, 2); |
| 286 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
| 287 | for (size_t i = 0; i < c; i++, r++) { |
| 288 | position[i * 6 + 0].x = r->left; |
| 289 | position[i * 6 + 0].y = height - r->top; |
| 290 | position[i * 6 + 1].x = r->left; |
| 291 | position[i * 6 + 1].y = height - r->bottom; |
| 292 | position[i * 6 + 2].x = r->right; |
| 293 | position[i * 6 + 2].y = height - r->bottom; |
| 294 | position[i * 6 + 3].x = r->left; |
| 295 | position[i * 6 + 3].y = height - r->top; |
| 296 | position[i * 6 + 4].x = r->right; |
| 297 | position[i * 6 + 4].y = height - r->bottom; |
| 298 | position[i * 6 + 5].x = r->right; |
| 299 | position[i * 6 + 5].y = height - r->top; |
| 300 | } |
| 301 | setupFillWithColor(red, green, blue, alpha); |
| 302 | drawMesh(mesh); |
| 303 | } |
| 304 | |
| 305 | void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) { |
| 306 | glClearColor(red, green, blue, alpha); |
| 307 | glClear(GL_COLOR_BUFFER_BIT); |
| 308 | } |
| 309 | |
| 310 | void GLES20RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) { |
| 311 | glScissor(left, bottom, right, top); |
| 312 | glEnable(GL_SCISSOR_TEST); |
| 313 | } |
| 314 | |
| 315 | void GLES20RenderEngine::disableScissor() { |
| 316 | glDisable(GL_SCISSOR_TEST); |
| 317 | } |
| 318 | |
| 319 | void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) { |
| 320 | glGenTextures(count, names); |
| 321 | } |
| 322 | |
| 323 | void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) { |
| 324 | glDeleteTextures(count, names); |
| 325 | } |
| 326 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 327 | void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName, |
| 328 | const Image& image) { |
| 329 | const GLImage& glImage = static_cast<const GLImage&>(image); |
| 330 | const GLenum target = GL_TEXTURE_EXTERNAL_OES; |
| 331 | |
| 332 | glBindTexture(target, texName); |
| 333 | if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) { |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 334 | glEGLImageTargetTexture2DOES(target, |
| 335 | static_cast<GLeglImageOES>(glImage.getEGLImage())); |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 336 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 339 | void GLES20RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) { |
| 340 | glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 341 | } |
| 342 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 343 | status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) { |
| 344 | GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer); |
| 345 | EGLImageKHR eglImage = glFramebuffer->getEGLImage(); |
| 346 | uint32_t textureName = glFramebuffer->getTextureName(); |
| 347 | uint32_t framebufferName = glFramebuffer->getFramebufferName(); |
| 348 | |
| 349 | // Bind the texture and turn our EGLImage into a texture |
| 350 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 351 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage); |
| 352 | |
| 353 | // Bind the Framebuffer to render into |
| 354 | glBindFramebuffer(GL_FRAMEBUFFER, framebufferName); |
| 355 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 356 | GL_TEXTURE_2D, textureName, 0); |
| 357 | |
| 358 | mRenderToFbo = true; |
| 359 | |
| 360 | uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 361 | |
| 362 | ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, |
| 363 | "glCheckFramebufferStatusOES error %d", glStatus); |
| 364 | |
| 365 | return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE; |
| 366 | } |
| 367 | |
| 368 | void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) { |
| 369 | mRenderToFbo = false; |
| 370 | |
| 371 | // back to main framebuffer |
| 372 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 373 | |
| 374 | // Workaround for b/77935566 to force the EGL driver to release the |
| 375 | // screenshot buffer |
| 376 | setScissor(0, 0, 0, 0); |
| 377 | clearWithColor(0.0, 0.0, 0.0, 0.0); |
| 378 | disableScissor(); |
| 379 | } |
| 380 | |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 381 | void GLES20RenderEngine::checkErrors() const { |
| 382 | do { |
| 383 | // there could be more than one error flag |
| 384 | GLenum error = glGetError(); |
| 385 | if (error == GL_NO_ERROR) break; |
| 386 | ALOGE("GL error 0x%04x", int(error)); |
| 387 | } while (true); |
| 388 | } |
| 389 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 390 | void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 391 | ui::Transform::orientation_flags rotation) { |
Ivan Lozano | 1f58ac5 | 2017-12-14 13:27:10 -0800 | [diff] [blame] | 392 | int32_t l = sourceCrop.left; |
| 393 | int32_t r = sourceCrop.right; |
Chia-I Wu | 1be50b5 | 2018-08-29 10:44:48 -0700 | [diff] [blame] | 394 | int32_t b = sourceCrop.bottom; |
| 395 | int32_t t = sourceCrop.top; |
| 396 | if (mRenderToFbo) { |
| 397 | std::swap(t, b); |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 398 | } |
Chia-I Wu | 1be50b5 | 2018-08-29 10:44:48 -0700 | [diff] [blame] | 399 | mat4 m = mat4::ortho(l, r, b, t, 0, 1); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 400 | |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 401 | // Apply custom rotation to the projection. |
| 402 | float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f; |
| 403 | switch (rotation) { |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 404 | case ui::Transform::ROT_0: |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 405 | break; |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 406 | case ui::Transform::ROT_90: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 407 | m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m; |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 408 | break; |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 409 | case ui::Transform::ROT_180: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 410 | m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m; |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 411 | break; |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 412 | case ui::Transform::ROT_270: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 413 | m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m; |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | break; |
| 417 | } |
| 418 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 419 | glViewport(0, 0, vpw, vph); |
| 420 | mState.setProjectionMatrix(m); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 421 | mVpWidth = vpw; |
| 422 | mVpHeight = vph; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 425 | void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, |
| 426 | bool disableTexture, const half4& color) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 427 | mState.setPremultipliedAlpha(premultipliedAlpha); |
| 428 | mState.setOpaque(opaque); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 429 | mState.setColor(color); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 430 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 431 | if (disableTexture) { |
| 432 | mState.disableTexture(); |
| 433 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 434 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 435 | if (color.a < 1.0f || !opaque) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 436 | glEnable(GL_BLEND); |
| 437 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 438 | } else { |
| 439 | glDisable(GL_BLEND); |
| 440 | } |
| 441 | } |
| 442 | |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 443 | void GLES20RenderEngine::setSourceY410BT2020(bool enable) { |
| 444 | mState.setY410BT2020(enable); |
| 445 | } |
| 446 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 447 | void GLES20RenderEngine::setSourceDataSpace(Dataspace source) { |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 448 | mDataSpace = source; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 449 | } |
| 450 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 451 | void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) { |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 452 | mOutputDataSpace = dataspace; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 453 | } |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 454 | |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 455 | void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) { |
| 456 | mState.setDisplayMaxLuminance(maxLuminance); |
| 457 | } |
| 458 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 459 | void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { |
| 460 | GLuint target = texture.getTextureTarget(); |
| 461 | glBindTexture(target, texture.getTextureName()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 462 | GLenum filter = GL_NEAREST; |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 463 | if (texture.getFiltering()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 464 | filter = GL_LINEAR; |
| 465 | } |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 466 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 467 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 468 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); |
| 469 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 470 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 471 | mState.setTexture(texture); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void GLES20RenderEngine::setupLayerBlackedOut() { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 475 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 476 | Texture texture(Texture::TEXTURE_2D, mProtectedTexName); |
| 477 | texture.setDimensions(1, 1); // FIXME: we should get that from somewhere |
| 478 | mState.setTexture(texture); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Chia-I Wu | 8e50e69 | 2018-05-04 10:12:37 -0700 | [diff] [blame] | 481 | void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) { |
Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 482 | mState.setColorMatrix(colorTransform); |
Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 485 | void GLES20RenderEngine::disableTexturing() { |
| 486 | mState.disableTexture(); |
| 487 | } |
| 488 | |
| 489 | void GLES20RenderEngine::disableBlending() { |
| 490 | glDisable(GL_BLEND); |
| 491 | } |
| 492 | |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 493 | void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 494 | mState.setPremultipliedAlpha(true); |
| 495 | mState.setOpaque(false); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 496 | mState.setColor(half4(r, g, b, a)); |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 497 | mState.disableTexture(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 498 | glDisable(GL_BLEND); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 502 | ATRACE_CALL(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 503 | if (mesh.getTexCoordsSize()) { |
| 504 | glEnableVertexAttribArray(Program::texCoords); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 505 | glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE, |
| 506 | mesh.getByteStride(), mesh.getTexCoords()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 509 | glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, |
| 510 | mesh.getByteStride(), mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 511 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 512 | // By default, DISPLAY_P3 is the only supported wide color output. However, |
| 513 | // when HDR content is present, hardware composer may be able to handle |
| 514 | // BT2020 data space, in that case, the output data space is set to be |
| 515 | // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need |
| 516 | // to respect this and convert non-HDR content to HDR format. |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 517 | if (mUseColorManagement) { |
| 518 | Description managedState = mState; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 519 | Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK); |
| 520 | Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); |
| 521 | Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace & |
| 522 | Dataspace::STANDARD_MASK); |
| 523 | Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace & |
| 524 | Dataspace::TRANSFER_MASK); |
| 525 | bool needsXYZConversion = needsXYZTransformMatrix(); |
| 526 | |
| 527 | if (needsXYZConversion) { |
| 528 | // The supported input color spaces are standard RGB, Display P3 and BT2020. |
| 529 | switch (inputStandard) { |
| 530 | case Dataspace::STANDARD_DCI_P3: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 531 | managedState.setInputTransformMatrix(mDisplayP3ToXyz); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 532 | break; |
| 533 | case Dataspace::STANDARD_BT2020: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 534 | managedState.setInputTransformMatrix(mBt2020ToXyz); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 535 | break; |
| 536 | default: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 537 | managedState.setInputTransformMatrix(mSrgbToXyz); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 541 | // The supported output color spaces are BT2020, Display P3 and standard RGB. |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 542 | switch (outputStandard) { |
| 543 | case Dataspace::STANDARD_BT2020: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 544 | managedState.setOutputTransformMatrix(mXyzToBt2020); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 545 | break; |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 546 | case Dataspace::STANDARD_DCI_P3: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 547 | managedState.setOutputTransformMatrix(mXyzToDisplayP3); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 548 | break; |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 549 | default: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 550 | managedState.setOutputTransformMatrix(mXyzToSrgb); |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 551 | break; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 552 | } |
| 553 | } else if (inputStandard != outputStandard) { |
| 554 | // At this point, the input data space and output data space could be both |
| 555 | // HDR data spaces, but they match each other, we do nothing in this case. |
| 556 | // In addition to the case above, the input data space could be |
| 557 | // - scRGB linear |
| 558 | // - scRGB non-linear |
| 559 | // - sRGB |
| 560 | // - Display P3 |
| 561 | // The output data spaces could be |
| 562 | // - sRGB |
| 563 | // - Display P3 |
| 564 | if (outputStandard == Dataspace::STANDARD_BT709) { |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 565 | managedState.setOutputTransformMatrix(mDisplayP3ToSrgb); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 566 | } else if (outputStandard == Dataspace::STANDARD_DCI_P3) { |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 567 | managedState.setOutputTransformMatrix(mSrgbToDisplayP3); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 568 | } |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 569 | } |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 570 | |
| 571 | // we need to convert the RGB value to linear space and convert it back when: |
| 572 | // - there is a color matrix that is not an identity matrix, or |
| 573 | // - there is an output transform matrix that is not an identity matrix, or |
| 574 | // - the input transfer function doesn't match the output transfer function. |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 575 | if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() || |
Chia-I Wu | d49d669 | 2018-06-27 07:17:41 +0800 | [diff] [blame] | 576 | inputTransfer != outputTransfer) { |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 577 | switch (inputTransfer) { |
| 578 | case Dataspace::TRANSFER_ST2084: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 579 | managedState.setInputTransferFunction(Description::TransferFunction::ST2084); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 580 | break; |
| 581 | case Dataspace::TRANSFER_HLG: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 582 | managedState.setInputTransferFunction(Description::TransferFunction::HLG); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 583 | break; |
| 584 | case Dataspace::TRANSFER_LINEAR: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 585 | managedState.setInputTransferFunction(Description::TransferFunction::LINEAR); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 586 | break; |
| 587 | default: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 588 | managedState.setInputTransferFunction(Description::TransferFunction::SRGB); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | |
| 592 | switch (outputTransfer) { |
| 593 | case Dataspace::TRANSFER_ST2084: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 594 | managedState.setOutputTransferFunction(Description::TransferFunction::ST2084); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 595 | break; |
| 596 | case Dataspace::TRANSFER_HLG: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 597 | managedState.setOutputTransferFunction(Description::TransferFunction::HLG); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 598 | break; |
| 599 | default: |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 600 | managedState.setOutputTransferFunction(Description::TransferFunction::SRGB); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 605 | ProgramCache::getInstance().useProgram(managedState); |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 606 | |
| 607 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 608 | |
| 609 | if (outputDebugPPMs) { |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 610 | static uint64_t managedColorFrameCount = 0; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 611 | std::ostringstream out; |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 612 | out << "/data/texture_out" << managedColorFrameCount++; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 613 | writePPM(out.str().c_str(), mVpWidth, mVpHeight); |
| 614 | } |
| 615 | } else { |
| 616 | ProgramCache::getInstance().useProgram(mState); |
| 617 | |
| 618 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 619 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 620 | |
| 621 | if (mesh.getTexCoordsSize()) { |
| 622 | glDisableVertexAttribArray(Program::texCoords); |
| 623 | } |
| 624 | } |
| 625 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 626 | size_t GLES20RenderEngine::getMaxTextureSize() const { |
| 627 | return mMaxTextureSize; |
| 628 | } |
| 629 | |
| 630 | size_t GLES20RenderEngine::getMaxViewportDims() const { |
| 631 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 632 | } |
| 633 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 634 | void GLES20RenderEngine::dump(String8& result) { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 635 | RenderEngine::dump(result); |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 636 | result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n", |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 637 | dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), |
| 638 | dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 641 | bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const { |
| 642 | const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK); |
| 643 | const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK); |
| 644 | return standard == Dataspace::STANDARD_BT2020 && |
| 645 | (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG); |
| 646 | } |
| 647 | |
| 648 | // For convenience, we want to convert the input color space to XYZ color space first, |
| 649 | // and then convert from XYZ color space to output color space when |
| 650 | // - SDR and HDR contents are mixed, either SDR content will be converted to HDR or |
| 651 | // HDR content will be tone-mapped to SDR; Or, |
| 652 | // - there are HDR PQ and HLG contents presented at the same time, where we want to convert |
| 653 | // HLG content to PQ content. |
| 654 | // In either case above, we need to operate the Y value in XYZ color space. Thus, when either |
| 655 | // input data space or output data space is HDR data space, and the input transfer function |
| 656 | // doesn't match the output transfer function, we would enable an intermediate transfrom to |
| 657 | // XYZ color space. |
| 658 | bool GLES20RenderEngine::needsXYZTransformMatrix() const { |
| 659 | const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace); |
| 660 | const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace); |
| 661 | const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); |
| 662 | const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace & |
| 663 | Dataspace::TRANSFER_MASK); |
| 664 | |
| 665 | return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer; |
| 666 | } |
| 667 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 668 | } // namespace gl |
| 669 | } // namespace renderengine |
| 670 | } // namespace android |