| 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) | 
|  | 115 | : mVpWidth(0), mVpHeight(0), mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 116 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); | 
|  | 117 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); | 
|  | 118 |  | 
|  | 119 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); | 
|  | 120 | glPixelStorei(GL_PACK_ALIGNMENT, 4); | 
|  | 121 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 122 | const uint16_t protTexData[] = {0}; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 123 | glGenTextures(1, &mProtectedTexName); | 
|  | 124 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); | 
|  | 125 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 126 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 127 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | 
|  | 128 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 129 | 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] | 130 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 131 | // mColorBlindnessCorrection = M; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 132 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 133 | if (mPlatformHasWideColor) { | 
|  | 134 | // Compute sRGB to DisplayP3 color transform | 
|  | 135 | // NOTE: For now, we are limiting wide-color support to | 
|  | 136 | // Display-P3 only. | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 137 | mSrgbToDisplayP3 = mat4( | 
|  | 138 | ColorSpaceConnector(ColorSpace::sRGB(), ColorSpace::DisplayP3()).getTransform()); | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 139 |  | 
|  | 140 | // Compute BT2020 to DisplayP3 color transform | 
|  | 141 | mBt2020ToDisplayP3 = mat4( | 
|  | 142 | ColorSpaceConnector(ColorSpace::BT2020(), ColorSpace::DisplayP3()).getTransform()); | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 143 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 146 | GLES20RenderEngine::~GLES20RenderEngine() {} | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 147 |  | 
|  | 148 | size_t GLES20RenderEngine::getMaxTextureSize() const { | 
|  | 149 | return mMaxTextureSize; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | size_t GLES20RenderEngine::getMaxViewportDims() const { | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 153 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1]; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 156 | void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, | 
|  | 157 | size_t hwh, bool yswap, | 
|  | 158 | Transform::orientation_flags rotation) { | 
| Ivan Lozano | 1f58ac5 | 2017-12-14 13:27:10 -0800 | [diff] [blame] | 159 | int32_t l = sourceCrop.left; | 
|  | 160 | int32_t r = sourceCrop.right; | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 161 |  | 
|  | 162 | // 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] | 163 | int32_t t = hwh - sourceCrop.top; | 
|  | 164 | int32_t b = hwh - sourceCrop.bottom; | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 165 |  | 
| Mathias Agopian | a8c386f | 2013-08-26 20:42:07 -0700 | [diff] [blame] | 166 | mat4 m; | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 167 | if (yswap) { | 
|  | 168 | m = mat4::ortho(l, r, t, b, 0, 1); | 
|  | 169 | } else { | 
|  | 170 | m = mat4::ortho(l, r, b, t, 0, 1); | 
|  | 171 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 172 |  | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 173 | // Apply custom rotation to the projection. | 
|  | 174 | float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f; | 
|  | 175 | switch (rotation) { | 
|  | 176 | case Transform::ROT_0: | 
|  | 177 | break; | 
|  | 178 | case Transform::ROT_90: | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 179 | m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m; | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 180 | break; | 
|  | 181 | case Transform::ROT_180: | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 182 | m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m; | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 183 | break; | 
|  | 184 | case Transform::ROT_270: | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 185 | m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m; | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 186 | break; | 
|  | 187 | default: | 
|  | 188 | break; | 
|  | 189 | } | 
|  | 190 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 191 | glViewport(0, 0, vpw, vph); | 
|  | 192 | mState.setProjectionMatrix(m); | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 193 | mVpWidth = vpw; | 
|  | 194 | mVpHeight = vph; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 197 | void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, | 
|  | 198 | bool disableTexture, const half4& color) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 199 | mState.setPremultipliedAlpha(premultipliedAlpha); | 
|  | 200 | mState.setOpaque(opaque); | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 201 | mState.setColor(color); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 202 |  | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 203 | if (disableTexture) { | 
|  | 204 | mState.disableTexture(); | 
|  | 205 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 206 |  | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 207 | if (color.a < 1.0f || !opaque) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 208 | glEnable(GL_BLEND); | 
|  | 209 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|  | 210 | } else { | 
|  | 211 | glDisable(GL_BLEND); | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 215 | void GLES20RenderEngine::setSourceY410BT2020(bool enable) { | 
|  | 216 | mState.setY410BT2020(enable); | 
|  | 217 | } | 
|  | 218 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 219 | void GLES20RenderEngine::setSourceDataSpace(Dataspace source) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 220 | mDataSpace = source; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 223 | void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 224 | mOutputDataSpace = dataspace; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 225 | } | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 226 |  | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 227 | void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) { | 
|  | 228 | mState.setDisplayMaxLuminance(maxLuminance); | 
|  | 229 | } | 
|  | 230 |  | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 231 | void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { | 
|  | 232 | GLuint target = texture.getTextureTarget(); | 
|  | 233 | glBindTexture(target, texture.getTextureName()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 234 | GLenum filter = GL_NEAREST; | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 235 | if (texture.getFiltering()) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 236 | filter = GL_LINEAR; | 
|  | 237 | } | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 238 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 239 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
|  | 240 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); | 
|  | 241 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 242 |  | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 243 | mState.setTexture(texture); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
|  | 246 | void GLES20RenderEngine::setupLayerBlackedOut() { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 247 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 248 | Texture texture(Texture::TEXTURE_2D, mProtectedTexName); | 
|  | 249 | texture.setDimensions(1, 1); // FIXME: we should get that from somewhere | 
|  | 250 | mState.setTexture(texture); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 253 | mat4 GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) { | 
|  | 254 | mat4 oldTransform = mState.getColorMatrix(); | 
|  | 255 | mState.setColorMatrix(colorTransform); | 
|  | 256 | return oldTransform; | 
|  | 257 | } | 
|  | 258 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 259 | void GLES20RenderEngine::disableTexturing() { | 
|  | 260 | mState.disableTexture(); | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | void GLES20RenderEngine::disableBlending() { | 
|  | 264 | glDisable(GL_BLEND); | 
|  | 265 | } | 
|  | 266 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 267 | void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, | 
|  | 268 | uint32_t* fbName, uint32_t* status) { | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 269 | GLuint tname, name; | 
|  | 270 | // turn our EGLImage into a texture | 
|  | 271 | glGenTextures(1, &tname); | 
|  | 272 | glBindTexture(GL_TEXTURE_2D, tname); | 
|  | 273 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); | 
|  | 274 |  | 
|  | 275 | // create a Framebuffer Object to render into | 
|  | 276 | glGenFramebuffers(1, &name); | 
|  | 277 | glBindFramebuffer(GL_FRAMEBUFFER, name); | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 278 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0); | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 279 |  | 
|  | 280 | *status = glCheckFramebufferStatus(GL_FRAMEBUFFER); | 
|  | 281 | *texName = tname; | 
|  | 282 | *fbName = name; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { | 
|  | 286 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 
|  | 287 | glDeleteFramebuffers(1, &fbName); | 
|  | 288 | glDeleteTextures(1, &texName); | 
|  | 289 | } | 
|  | 290 |  | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 291 | void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 292 | mState.setPremultipliedAlpha(true); | 
|  | 293 | mState.setOpaque(false); | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 294 | mState.setColor(half4(r, g, b, a)); | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 295 | mState.disableTexture(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 296 | glDisable(GL_BLEND); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 297 | } | 
|  | 298 |  | 
|  | 299 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 300 | ATRACE_CALL(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 301 | if (mesh.getTexCoordsSize()) { | 
|  | 302 | glEnableVertexAttribArray(Program::texCoords); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 303 | glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE, | 
|  | 304 | mesh.getByteStride(), mesh.getTexCoords()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 305 | } | 
|  | 306 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 307 | glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, | 
|  | 308 | mesh.getByteStride(), mesh.getPositions()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 309 |  | 
| Peiyong Lin | 0e7a791 | 2018-04-05 14:36:36 -0700 | [diff] [blame] | 310 | // TODO(b/73825729) Refactor this code block to handle BT2020 color space properly. | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 311 | // DISPLAY_P3 is the only supported wide color output | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 312 | if (mPlatformHasWideColor && mOutputDataSpace == Dataspace::DISPLAY_P3) { | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 313 | Description wideColorState = mState; | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 314 | switch (mDataSpace) { | 
|  | 315 | case Dataspace::DISPLAY_P3: | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 316 | // input matches output | 
|  | 317 | break; | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 318 | case Dataspace::BT2020_PQ: | 
|  | 319 | case Dataspace::BT2020_ITU_PQ: | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 320 | wideColorState.setColorMatrix(mState.getColorMatrix() * mBt2020ToDisplayP3); | 
|  | 321 | wideColorState.setInputTransferFunction(Description::TransferFunction::ST2084); | 
|  | 322 | wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB); | 
| Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 323 | break; | 
|  | 324 | case Dataspace::BT2020_HLG: | 
|  | 325 | case Dataspace::BT2020_ITU_HLG: | 
|  | 326 | wideColorState.setColorMatrix(mState.getColorMatrix() * mBt2020ToDisplayP3); | 
|  | 327 | wideColorState.setInputTransferFunction(Description::TransferFunction::HLG); | 
|  | 328 | wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB); | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 329 | break; | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 330 | default: | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 331 | // treat all other dataspaces as sRGB | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 332 | wideColorState.setColorMatrix(mState.getColorMatrix() * mSrgbToDisplayP3); | 
| Chia-I Wu | db06e5e | 2018-04-13 21:47:20 -0700 | [diff] [blame] | 333 | switch (static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK)) { | 
|  | 334 | case Dataspace::TRANSFER_LINEAR: | 
|  | 335 | wideColorState.setInputTransferFunction( | 
|  | 336 | Description::TransferFunction::LINEAR); | 
|  | 337 | break; | 
|  | 338 | default: | 
|  | 339 | // treat all other transfer functions as sRGB | 
|  | 340 | wideColorState.setInputTransferFunction( | 
|  | 341 | Description::TransferFunction::SRGB); | 
|  | 342 | break; | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 343 | } | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 344 | wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB); | 
|  | 345 | ALOGV("drawMesh: gamut transform applied"); | 
|  | 346 | break; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 347 | } | 
|  | 348 | ProgramCache::getInstance().useProgram(wideColorState); | 
|  | 349 |  | 
|  | 350 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); | 
|  | 351 |  | 
|  | 352 | if (outputDebugPPMs) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 353 | static uint64_t wideColorFrameCount = 0; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 354 | std::ostringstream out; | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 355 | out << "/data/texture_out" << wideColorFrameCount++; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 356 | writePPM(out.str().c_str(), mVpWidth, mVpHeight); | 
|  | 357 | } | 
|  | 358 | } else { | 
|  | 359 | ProgramCache::getInstance().useProgram(mState); | 
|  | 360 |  | 
|  | 361 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); | 
|  | 362 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 363 |  | 
|  | 364 | if (mesh.getTexCoordsSize()) { | 
|  | 365 | glDisableVertexAttribArray(Program::texCoords); | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | void GLES20RenderEngine::dump(String8& result) { | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 370 | RenderEngine::dump(result); | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 371 | result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n", | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 372 | dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), | 
|  | 373 | dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 374 | } | 
|  | 375 |  | 
|  | 376 | // --------------------------------------------------------------------------- | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 377 | } // namespace impl | 
|  | 378 | } // namespace RE | 
|  | 379 | } // namespace android | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 380 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 381 |  | 
|  | 382 | #if defined(__gl_h_) | 
|  | 383 | #error "don't include gl/gl.h in this file" | 
|  | 384 | #endif |