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