| 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 |  | 
|  | 22 | #include <GLES2/gl2.h> | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 23 | #include <GLES2/gl2ext.h> | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 24 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 25 | #include <ui/ColorSpace.h> | 
|  | 26 | #include <ui/DebugUtils.h> | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 27 | #include <ui/Rect.h> | 
|  | 28 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 29 | #include <utils/String8.h> | 
|  | 30 | #include <utils/Trace.h> | 
|  | 31 |  | 
|  | 32 | #include <cutils/compiler.h> | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 33 | #include <gui/ISurfaceComposer.h> | 
|  | 34 | #include <math.h> | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 35 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 36 | #include "Description.h" | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 37 | #include "GLES20RenderEngine.h" | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 38 | #include "Mesh.h" | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 39 | #include "Program.h" | 
|  | 40 | #include "ProgramCache.h" | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 41 | #include "Texture.h" | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 42 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 43 | #include <fstream> | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 44 | #include <sstream> | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 45 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 46 | // --------------------------------------------------------------------------- | 
|  | 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 | // --------------------------------------------------------------------------- | 
|  | 107 | namespace android { | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 108 | namespace RE { | 
|  | 109 | namespace impl { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 110 | // --------------------------------------------------------------------------- | 
|  | 111 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 112 | using ui::Dataspace; | 
|  | 113 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 114 | GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags) | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 115 | : RenderEngine(featureFlags), | 
|  | 116 | mVpWidth(0), | 
|  | 117 | mVpHeight(0), | 
|  | 118 | mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 119 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); | 
|  | 120 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); | 
|  | 121 |  | 
|  | 122 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); | 
|  | 123 | glPixelStorei(GL_PACK_ALIGNMENT, 4); | 
|  | 124 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 125 | const uint16_t protTexData[] = {0}; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 126 | glGenTextures(1, &mProtectedTexName); | 
|  | 127 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); | 
|  | 128 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 130 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | 
|  | 131 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 132 | 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] | 133 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 134 | // mColorBlindnessCorrection = M; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 135 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 136 | if (mPlatformHasWideColor) { | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 137 | ColorSpace srgb(ColorSpace::sRGB()); | 
|  | 138 | ColorSpace displayP3(ColorSpace::DisplayP3()); | 
|  | 139 | ColorSpace bt2020(ColorSpace::BT2020()); | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 140 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 141 | // Compute sRGB to Display P3 transform matrix. | 
|  | 142 | // NOTE: For now, we are limiting output wide color space support to | 
|  | 143 | // Display-P3 only. | 
|  | 144 | mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform()); | 
|  | 145 |  | 
|  | 146 | // Compute Display P3 to sRGB transform matrix. | 
|  | 147 | mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform()); | 
|  | 148 |  | 
|  | 149 | // no chromatic adaptation needed since all color spaces use D65 for their white points. | 
|  | 150 | mSrgbToXyz = srgb.getRGBtoXYZ(); | 
|  | 151 | mDisplayP3ToXyz = displayP3.getRGBtoXYZ(); | 
|  | 152 | mBt2020ToXyz = bt2020.getRGBtoXYZ(); | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 153 | mXyzToSrgb = mat4(srgb.getXYZtoRGB()); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 154 | mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB()); | 
|  | 155 | mXyzToBt2020 = mat4(bt2020.getXYZtoRGB()); | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 156 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 159 | GLES20RenderEngine::~GLES20RenderEngine() {} | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | size_t GLES20RenderEngine::getMaxTextureSize() const { | 
|  | 162 | return mMaxTextureSize; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | size_t GLES20RenderEngine::getMaxViewportDims() const { | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 166 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1]; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 169 | void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, | 
|  | 170 | size_t hwh, bool yswap, | 
|  | 171 | Transform::orientation_flags rotation) { | 
| Ivan Lozano | 1f58ac5 | 2017-12-14 13:27:10 -0800 | [diff] [blame] | 172 | int32_t l = sourceCrop.left; | 
|  | 173 | int32_t r = sourceCrop.right; | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 174 |  | 
|  | 175 | // In GL, (0, 0) is the bottom-left corner, so flip y coordinates | 
| Ivan Lozano | 1f58ac5 | 2017-12-14 13:27:10 -0800 | [diff] [blame] | 176 | int32_t t = hwh - sourceCrop.top; | 
|  | 177 | int32_t b = hwh - sourceCrop.bottom; | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 178 |  | 
| Mathias Agopian | a8c386f | 2013-08-26 20:42:07 -0700 | [diff] [blame] | 179 | mat4 m; | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 180 | if (yswap) { | 
|  | 181 | m = mat4::ortho(l, r, t, b, 0, 1); | 
|  | 182 | } else { | 
|  | 183 | m = mat4::ortho(l, r, b, t, 0, 1); | 
|  | 184 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 185 |  | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 186 | // Apply custom rotation to the projection. | 
|  | 187 | float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f; | 
|  | 188 | switch (rotation) { | 
|  | 189 | case Transform::ROT_0: | 
|  | 190 | break; | 
|  | 191 | case Transform::ROT_90: | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 192 | m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m; | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 193 | break; | 
|  | 194 | case Transform::ROT_180: | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 195 | m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m; | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 196 | break; | 
|  | 197 | case Transform::ROT_270: | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 198 | m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m; | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 199 | break; | 
|  | 200 | default: | 
|  | 201 | break; | 
|  | 202 | } | 
|  | 203 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 204 | glViewport(0, 0, vpw, vph); | 
|  | 205 | mState.setProjectionMatrix(m); | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 206 | mVpWidth = vpw; | 
|  | 207 | mVpHeight = vph; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 210 | void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, | 
|  | 211 | bool disableTexture, const half4& color) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 212 | mState.setPremultipliedAlpha(premultipliedAlpha); | 
|  | 213 | mState.setOpaque(opaque); | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 214 | mState.setColor(color); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 215 |  | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 216 | if (disableTexture) { | 
|  | 217 | mState.disableTexture(); | 
|  | 218 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 219 |  | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 220 | if (color.a < 1.0f || !opaque) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 221 | glEnable(GL_BLEND); | 
|  | 222 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|  | 223 | } else { | 
|  | 224 | glDisable(GL_BLEND); | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 |  | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 228 | void GLES20RenderEngine::setSourceY410BT2020(bool enable) { | 
|  | 229 | mState.setY410BT2020(enable); | 
|  | 230 | } | 
|  | 231 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 232 | void GLES20RenderEngine::setSourceDataSpace(Dataspace source) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 233 | mDataSpace = source; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 234 | } | 
|  | 235 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 236 | void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 237 | mOutputDataSpace = dataspace; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 238 | } | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 239 |  | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 240 | void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) { | 
|  | 241 | mState.setDisplayMaxLuminance(maxLuminance); | 
|  | 242 | } | 
|  | 243 |  | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 244 | void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { | 
|  | 245 | GLuint target = texture.getTextureTarget(); | 
|  | 246 | glBindTexture(target, texture.getTextureName()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 247 | GLenum filter = GL_NEAREST; | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 248 | if (texture.getFiltering()) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 249 | filter = GL_LINEAR; | 
|  | 250 | } | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 251 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 252 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
|  | 253 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); | 
|  | 254 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 255 |  | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 256 | mState.setTexture(texture); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
|  | 259 | void GLES20RenderEngine::setupLayerBlackedOut() { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 260 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 261 | Texture texture(Texture::TEXTURE_2D, mProtectedTexName); | 
|  | 262 | texture.setDimensions(1, 1); // FIXME: we should get that from somewhere | 
|  | 263 | mState.setTexture(texture); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
| Chia-I Wu | 8e50e69 | 2018-05-04 10:12:37 -0700 | [diff] [blame] | 266 | void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) { | 
| Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 267 | mState.setColorMatrix(colorTransform); | 
| Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 270 | void GLES20RenderEngine::disableTexturing() { | 
|  | 271 | mState.disableTexture(); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | void GLES20RenderEngine::disableBlending() { | 
|  | 275 | glDisable(GL_BLEND); | 
|  | 276 | } | 
|  | 277 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 278 | void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, | 
|  | 279 | uint32_t* fbName, uint32_t* status) { | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 280 | GLuint tname, name; | 
|  | 281 | // turn our EGLImage into a texture | 
|  | 282 | glGenTextures(1, &tname); | 
|  | 283 | glBindTexture(GL_TEXTURE_2D, tname); | 
|  | 284 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); | 
|  | 285 |  | 
|  | 286 | // create a Framebuffer Object to render into | 
|  | 287 | glGenFramebuffers(1, &name); | 
|  | 288 | glBindFramebuffer(GL_FRAMEBUFFER, name); | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 289 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0); | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 290 |  | 
|  | 291 | *status = glCheckFramebufferStatus(GL_FRAMEBUFFER); | 
|  | 292 | *texName = tname; | 
|  | 293 | *fbName = name; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { | 
|  | 297 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 
|  | 298 | glDeleteFramebuffers(1, &fbName); | 
|  | 299 | glDeleteTextures(1, &texName); | 
|  | 300 | } | 
|  | 301 |  | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 302 | void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 303 | mState.setPremultipliedAlpha(true); | 
|  | 304 | mState.setOpaque(false); | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 305 | mState.setColor(half4(r, g, b, a)); | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 306 | mState.disableTexture(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 307 | glDisable(GL_BLEND); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
|  | 310 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 311 | ATRACE_CALL(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 312 | if (mesh.getTexCoordsSize()) { | 
|  | 313 | glEnableVertexAttribArray(Program::texCoords); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 314 | glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE, | 
|  | 315 | mesh.getByteStride(), mesh.getTexCoords()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 316 | } | 
|  | 317 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 318 | glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, | 
|  | 319 | mesh.getByteStride(), mesh.getPositions()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 320 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 321 | // By default, DISPLAY_P3 is the only supported wide color output. However, | 
|  | 322 | // when HDR content is present, hardware composer may be able to handle | 
|  | 323 | // BT2020 data space, in that case, the output data space is set to be | 
|  | 324 | // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need | 
|  | 325 | // to respect this and convert non-HDR content to HDR format. | 
|  | 326 | if (mPlatformHasWideColor) { | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 327 | Description wideColorState = mState; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 328 | Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK); | 
|  | 329 | Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); | 
|  | 330 | Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace & | 
|  | 331 | Dataspace::STANDARD_MASK); | 
|  | 332 | Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace & | 
|  | 333 | Dataspace::TRANSFER_MASK); | 
|  | 334 | bool needsXYZConversion = needsXYZTransformMatrix(); | 
|  | 335 |  | 
|  | 336 | if (needsXYZConversion) { | 
|  | 337 | // The supported input color spaces are standard RGB, Display P3 and BT2020. | 
|  | 338 | switch (inputStandard) { | 
|  | 339 | case Dataspace::STANDARD_DCI_P3: | 
|  | 340 | wideColorState.setInputTransformMatrix(mDisplayP3ToXyz); | 
|  | 341 | break; | 
|  | 342 | case Dataspace::STANDARD_BT2020: | 
|  | 343 | wideColorState.setInputTransformMatrix(mBt2020ToXyz); | 
|  | 344 | break; | 
|  | 345 | default: | 
|  | 346 | wideColorState.setInputTransformMatrix(mSrgbToXyz); | 
|  | 347 | break; | 
|  | 348 | } | 
|  | 349 |  | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 350 | // The supported output color spaces are BT2020, Display P3 and standard RGB. | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 351 | switch (outputStandard) { | 
|  | 352 | case Dataspace::STANDARD_BT2020: | 
|  | 353 | wideColorState.setOutputTransformMatrix(mXyzToBt2020); | 
|  | 354 | break; | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 355 | case Dataspace::STANDARD_DCI_P3: | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 356 | wideColorState.setOutputTransformMatrix(mXyzToDisplayP3); | 
|  | 357 | break; | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 358 | default: | 
|  | 359 | wideColorState.setOutputTransformMatrix(mXyzToSrgb); | 
|  | 360 | break; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 361 | } | 
|  | 362 | } else if (inputStandard != outputStandard) { | 
|  | 363 | // At this point, the input data space and output data space could be both | 
|  | 364 | // HDR data spaces, but they match each other, we do nothing in this case. | 
|  | 365 | // In addition to the case above, the input data space could be | 
|  | 366 | // - scRGB linear | 
|  | 367 | // - scRGB non-linear | 
|  | 368 | // - sRGB | 
|  | 369 | // - Display P3 | 
|  | 370 | // The output data spaces could be | 
|  | 371 | // - sRGB | 
|  | 372 | // - Display P3 | 
|  | 373 | if (outputStandard == Dataspace::STANDARD_BT709) { | 
|  | 374 | wideColorState.setOutputTransformMatrix(mDisplayP3ToSrgb); | 
|  | 375 | } else if (outputStandard == Dataspace::STANDARD_DCI_P3) { | 
|  | 376 | wideColorState.setOutputTransformMatrix(mSrgbToDisplayP3); | 
|  | 377 | } | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 378 | } | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 379 |  | 
|  | 380 | // we need to convert the RGB value to linear space and convert it back when: | 
|  | 381 | // - there is a color matrix that is not an identity matrix, or | 
|  | 382 | // - there is an output transform matrix that is not an identity matrix, or | 
|  | 383 | // - the input transfer function doesn't match the output transfer function. | 
| Chia-I Wu | 6d84411 | 2018-06-27 07:17:41 +0800 | [diff] [blame] | 384 | if (wideColorState.hasColorMatrix() || wideColorState.hasOutputTransformMatrix() || | 
|  | 385 | inputTransfer != outputTransfer) { | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 386 | switch (inputTransfer) { | 
|  | 387 | case Dataspace::TRANSFER_ST2084: | 
|  | 388 | wideColorState.setInputTransferFunction(Description::TransferFunction::ST2084); | 
|  | 389 | break; | 
|  | 390 | case Dataspace::TRANSFER_HLG: | 
|  | 391 | wideColorState.setInputTransferFunction(Description::TransferFunction::HLG); | 
|  | 392 | break; | 
|  | 393 | case Dataspace::TRANSFER_LINEAR: | 
|  | 394 | wideColorState.setInputTransferFunction(Description::TransferFunction::LINEAR); | 
|  | 395 | break; | 
|  | 396 | default: | 
|  | 397 | wideColorState.setInputTransferFunction(Description::TransferFunction::SRGB); | 
|  | 398 | break; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | switch (outputTransfer) { | 
|  | 402 | case Dataspace::TRANSFER_ST2084: | 
|  | 403 | wideColorState.setOutputTransferFunction(Description::TransferFunction::ST2084); | 
|  | 404 | break; | 
|  | 405 | case Dataspace::TRANSFER_HLG: | 
|  | 406 | wideColorState.setOutputTransferFunction(Description::TransferFunction::HLG); | 
|  | 407 | break; | 
|  | 408 | default: | 
|  | 409 | wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB); | 
|  | 410 | break; | 
|  | 411 | } | 
|  | 412 | } | 
|  | 413 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 414 | ProgramCache::getInstance().useProgram(wideColorState); | 
|  | 415 |  | 
|  | 416 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); | 
|  | 417 |  | 
|  | 418 | if (outputDebugPPMs) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 419 | static uint64_t wideColorFrameCount = 0; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 420 | std::ostringstream out; | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 421 | out << "/data/texture_out" << wideColorFrameCount++; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 422 | writePPM(out.str().c_str(), mVpWidth, mVpHeight); | 
|  | 423 | } | 
|  | 424 | } else { | 
|  | 425 | ProgramCache::getInstance().useProgram(mState); | 
|  | 426 |  | 
|  | 427 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); | 
|  | 428 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 429 |  | 
|  | 430 | if (mesh.getTexCoordsSize()) { | 
|  | 431 | glDisableVertexAttribArray(Program::texCoords); | 
|  | 432 | } | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | void GLES20RenderEngine::dump(String8& result) { | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 436 | RenderEngine::dump(result); | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 437 | result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n", | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 438 | dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), | 
|  | 439 | dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 440 | } | 
|  | 441 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 442 | bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const { | 
|  | 443 | const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK); | 
|  | 444 | const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK); | 
|  | 445 | return standard == Dataspace::STANDARD_BT2020 && | 
|  | 446 | (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG); | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | // For convenience, we want to convert the input color space to XYZ color space first, | 
|  | 450 | // and then convert from XYZ color space to output color space when | 
|  | 451 | // - SDR and HDR contents are mixed, either SDR content will be converted to HDR or | 
|  | 452 | //   HDR content will be tone-mapped to SDR; Or, | 
|  | 453 | // - there are HDR PQ and HLG contents presented at the same time, where we want to convert | 
|  | 454 | //   HLG content to PQ content. | 
|  | 455 | // In either case above, we need to operate the Y value in XYZ color space. Thus, when either | 
|  | 456 | // input data space or output data space is HDR data space, and the input transfer function | 
|  | 457 | // doesn't match the output transfer function, we would enable an intermediate transfrom to | 
|  | 458 | // XYZ color space. | 
|  | 459 | bool GLES20RenderEngine::needsXYZTransformMatrix() const { | 
|  | 460 | const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace); | 
|  | 461 | const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace); | 
|  | 462 | const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); | 
|  | 463 | const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace & | 
|  | 464 | Dataspace::TRANSFER_MASK); | 
|  | 465 |  | 
|  | 466 | return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer; | 
|  | 467 | } | 
|  | 468 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 469 | // --------------------------------------------------------------------------- | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 470 | } // namespace impl | 
|  | 471 | } // namespace RE | 
|  | 472 | } // namespace android | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 473 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 474 |  | 
|  | 475 | #if defined(__gl_h_) | 
|  | 476 | #error "don't include gl/gl.h in this file" | 
|  | 477 | #endif |