| 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 |  | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 18 |  | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 19 | #include "ProgramCache.h" | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 20 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | #include <GLES2/gl2.h> | 
|  | 22 | #include <GLES2/gl2ext.h> | 
| Chia-I Wu | 56d7b0a | 2018-10-01 15:13:11 -0700 | [diff] [blame] | 23 | #include <log/log.h> | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 24 | #include <renderengine/private/Description.h> | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 25 | #include <utils/String8.h> | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 26 | #include <utils/Trace.h> | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 27 | #include "Program.h" | 
|  | 28 |  | 
|  | 29 | ANDROID_SINGLETON_STATIC_INSTANCE(android::renderengine::gl::ProgramCache) | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 30 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 31 | namespace android { | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 32 | namespace renderengine { | 
|  | 33 | namespace gl { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 34 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 35 | /* | 
|  | 36 | * A simple formatter class to automatically add the endl and | 
|  | 37 | * manage the indentation. | 
|  | 38 | */ | 
|  | 39 |  | 
|  | 40 | class Formatter; | 
|  | 41 | static Formatter& indent(Formatter& f); | 
|  | 42 | static Formatter& dedent(Formatter& f); | 
|  | 43 |  | 
|  | 44 | class Formatter { | 
|  | 45 | String8 mString; | 
|  | 46 | int mIndent; | 
|  | 47 | typedef Formatter& (*FormaterManipFunc)(Formatter&); | 
|  | 48 | friend Formatter& indent(Formatter& f); | 
|  | 49 | friend Formatter& dedent(Formatter& f); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 50 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 51 | public: | 
| Andy McFadden | 892f22d | 2013-08-15 10:05:01 -0700 | [diff] [blame] | 52 | Formatter() : mIndent(0) {} | 
|  | 53 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 54 | String8 getString() const { return mString; } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 55 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 56 | friend Formatter& operator<<(Formatter& out, const char* in) { | 
|  | 57 | for (int i = 0; i < out.mIndent; i++) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 58 | out.mString.append("    "); | 
|  | 59 | } | 
|  | 60 | out.mString.append(in); | 
|  | 61 | out.mString.append("\n"); | 
|  | 62 | return out; | 
|  | 63 | } | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 64 | friend inline Formatter& operator<<(Formatter& out, const String8& in) { | 
|  | 65 | return operator<<(out, in.string()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 66 | } | 
|  | 67 | friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) { | 
|  | 68 | return (*func)(to); | 
|  | 69 | } | 
|  | 70 | }; | 
|  | 71 | Formatter& indent(Formatter& f) { | 
|  | 72 | f.mIndent++; | 
|  | 73 | return f; | 
|  | 74 | } | 
|  | 75 | Formatter& dedent(Formatter& f) { | 
|  | 76 | f.mIndent--; | 
|  | 77 | return f; | 
|  | 78 | } | 
|  | 79 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 80 | void ProgramCache::primeCache(EGLContext context, bool useColorManagement) { | 
|  | 81 | auto& cache = mCaches[context]; | 
| Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 82 | uint32_t shaderCount = 0; | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 83 | uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK | 
|  | 84 | | Key::ROUNDED_CORNERS_MASK; | 
| Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 85 | // Prime the cache for all combinations of the above masks, | 
|  | 86 | // leaving off the experimental color matrix mask options. | 
|  | 87 |  | 
|  | 88 | nsecs_t timeBefore = systemTime(); | 
|  | 89 | for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) { | 
|  | 90 | Key shaderKey; | 
|  | 91 | shaderKey.set(keyMask, keyVal); | 
|  | 92 | uint32_t tex = shaderKey.getTextureTarget(); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 93 | if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) { | 
| Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 94 | continue; | 
|  | 95 | } | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 96 | if (cache.count(shaderKey) == 0) { | 
|  | 97 | cache.emplace(shaderKey, generateProgram(shaderKey)); | 
| Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 98 | shaderCount++; | 
|  | 99 | } | 
|  | 100 | } | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | // Prime for sRGB->P3 conversion | 
| Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 103 | if (useColorManagement) { | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 104 | Key shaderKey; | 
|  | 105 | shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK | | 
|  | 106 | Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK, | 
|  | 107 | Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON | | 
|  | 108 | Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB); | 
|  | 109 | for (int i = 0; i < 4; i++) { | 
|  | 110 | shaderKey.set(Key::OPACITY_MASK, | 
|  | 111 | (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT); | 
|  | 112 | shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE); | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 113 | if (cache.count(shaderKey) == 0) { | 
|  | 114 | cache.emplace(shaderKey, generateProgram(shaderKey)); | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 115 | shaderCount++; | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
| Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 120 | nsecs_t timeAfter = systemTime(); | 
|  | 121 | float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6; | 
|  | 122 | ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs); | 
|  | 123 | } | 
|  | 124 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 125 | ProgramCache::Key ProgramCache::computeKey(const Description& description) { | 
|  | 126 | Key needs; | 
|  | 127 | needs.set(Key::TEXTURE_MASK, | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 128 | !description.textureEnabled | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 129 | ? Key::TEXTURE_OFF | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 130 | : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 131 | ? Key::TEXTURE_EXT | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 132 | : description.texture.getTextureTarget() == GL_TEXTURE_2D | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 133 | ? Key::TEXTURE_2D | 
|  | 134 | : Key::TEXTURE_OFF) | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 135 | .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE) | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 136 | .set(Key::BLEND_MASK, | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 137 | description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL) | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 138 | .set(Key::OPACITY_MASK, | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 139 | description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT) | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 140 | .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK, | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 141 | description.hasInputTransformMatrix() | 
|  | 142 | ? Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF) | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 143 | .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK, | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 144 | description.hasOutputTransformMatrix() || description.hasColorMatrix() | 
|  | 145 | ? Key::OUTPUT_TRANSFORM_MATRIX_ON | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 146 | : Key::OUTPUT_TRANSFORM_MATRIX_OFF) | 
|  | 147 | .set(Key::ROUNDED_CORNERS_MASK, | 
|  | 148 | description.cornerRadius > 0 | 
|  | 149 | ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF); | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 150 |  | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 151 | needs.set(Key::Y410_BT2020_MASK, | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 152 | description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF); | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 153 |  | 
| Peiyong Lin | 646f934 | 2018-12-27 15:24:25 -0800 | [diff] [blame] | 154 | if (needs.hasTransformMatrix() || | 
|  | 155 | (description.inputTransferFunction != description.outputTransferFunction)) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 156 | switch (description.inputTransferFunction) { | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 157 | case Description::TransferFunction::LINEAR: | 
|  | 158 | default: | 
|  | 159 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR); | 
|  | 160 | break; | 
|  | 161 | case Description::TransferFunction::SRGB: | 
|  | 162 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB); | 
|  | 163 | break; | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 164 | case Description::TransferFunction::ST2084: | 
|  | 165 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084); | 
|  | 166 | break; | 
| Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 167 | case Description::TransferFunction::HLG: | 
|  | 168 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG); | 
|  | 169 | break; | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 172 | switch (description.outputTransferFunction) { | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 173 | case Description::TransferFunction::LINEAR: | 
|  | 174 | default: | 
|  | 175 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR); | 
|  | 176 | break; | 
|  | 177 | case Description::TransferFunction::SRGB: | 
|  | 178 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB); | 
|  | 179 | break; | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 180 | case Description::TransferFunction::ST2084: | 
|  | 181 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084); | 
|  | 182 | break; | 
| Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 183 | case Description::TransferFunction::HLG: | 
|  | 184 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG); | 
|  | 185 | break; | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 186 | } | 
|  | 187 | } | 
|  | 188 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 189 | return needs; | 
|  | 190 | } | 
|  | 191 |  | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 192 | // Generate EOTF that converts signal values to relative display light, | 
|  | 193 | // both normalized to [0, 1]. | 
|  | 194 | void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) { | 
|  | 195 | switch (needs.getInputTF()) { | 
|  | 196 | case Key::INPUT_TF_SRGB: | 
|  | 197 | fs << R"__SHADER__( | 
|  | 198 | float EOTF_sRGB(float srgb) { | 
|  | 199 | return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | vec3 EOTF_sRGB(const vec3 srgb) { | 
|  | 203 | return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b)); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | vec3 EOTF(const vec3 srgb) { | 
|  | 207 | return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb)); | 
|  | 208 | } | 
|  | 209 | )__SHADER__"; | 
|  | 210 | break; | 
|  | 211 | case Key::INPUT_TF_ST2084: | 
|  | 212 | fs << R"__SHADER__( | 
|  | 213 | vec3 EOTF(const highp vec3 color) { | 
|  | 214 | const highp float m1 = (2610.0 / 4096.0) / 4.0; | 
|  | 215 | const highp float m2 = (2523.0 / 4096.0) * 128.0; | 
|  | 216 | const highp float c1 = (3424.0 / 4096.0); | 
|  | 217 | const highp float c2 = (2413.0 / 4096.0) * 32.0; | 
|  | 218 | const highp float c3 = (2392.0 / 4096.0) * 32.0; | 
|  | 219 |  | 
| Siddharth Kapoor | ac1f7c9 | 2018-12-05 20:29:06 +0800 | [diff] [blame] | 220 | highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2)); | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 221 | tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp); | 
|  | 222 | return pow(tmp, 1.0 / vec3(m1)); | 
|  | 223 | } | 
|  | 224 | )__SHADER__"; | 
|  | 225 | break; | 
|  | 226 | case Key::INPUT_TF_HLG: | 
|  | 227 | fs << R"__SHADER__( | 
|  | 228 | highp float EOTF_channel(const highp float channel) { | 
|  | 229 | const highp float a = 0.17883277; | 
|  | 230 | const highp float b = 0.28466892; | 
|  | 231 | const highp float c = 0.55991073; | 
|  | 232 | return channel <= 0.5 ? channel * channel / 3.0 : | 
|  | 233 | (exp((channel - c) / a) + b) / 12.0; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | vec3 EOTF(const highp vec3 color) { | 
|  | 237 | return vec3(EOTF_channel(color.r), EOTF_channel(color.g), | 
|  | 238 | EOTF_channel(color.b)); | 
|  | 239 | } | 
|  | 240 | )__SHADER__"; | 
|  | 241 | break; | 
|  | 242 | default: | 
|  | 243 | fs << R"__SHADER__( | 
|  | 244 | vec3 EOTF(const vec3 linear) { | 
|  | 245 | return linear; | 
|  | 246 | } | 
|  | 247 | )__SHADER__"; | 
|  | 248 | break; | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 |  | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 252 | void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) { | 
|  | 253 | // Convert relative light to absolute light. | 
|  | 254 | switch (needs.getInputTF()) { | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 255 | case Key::INPUT_TF_ST2084: | 
|  | 256 | fs << R"__SHADER__( | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 257 | highp vec3 ScaleLuminance(highp vec3 color) { | 
|  | 258 | return color * 10000.0; | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 259 | } | 
|  | 260 | )__SHADER__"; | 
|  | 261 | break; | 
|  | 262 | case Key::INPUT_TF_HLG: | 
|  | 263 | fs << R"__SHADER__( | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 264 | highp vec3 ScaleLuminance(highp vec3 color) { | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 265 | // The formula is: | 
|  | 266 | // alpha * pow(Y, gamma - 1.0) * color + beta; | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 267 | // where alpha is 1000.0, gamma is 1.2, beta is 0.0. | 
|  | 268 | return color * 1000.0 * pow(color.y, 0.2); | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 269 | } | 
|  | 270 | )__SHADER__"; | 
|  | 271 | break; | 
|  | 272 | default: | 
|  | 273 | fs << R"__SHADER__( | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 274 | highp vec3 ScaleLuminance(highp vec3 color) { | 
|  | 275 | return color * displayMaxLuminance; | 
|  | 276 | } | 
|  | 277 | )__SHADER__"; | 
|  | 278 | break; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | // Tone map absolute light to display luminance range. | 
|  | 282 | switch (needs.getInputTF()) { | 
|  | 283 | case Key::INPUT_TF_ST2084: | 
|  | 284 | case Key::INPUT_TF_HLG: | 
|  | 285 | switch (needs.getOutputTF()) { | 
|  | 286 | case Key::OUTPUT_TF_HLG: | 
|  | 287 | // Right now when mixed PQ and HLG contents are presented, | 
|  | 288 | // HLG content will always be converted to PQ. However, for | 
|  | 289 | // completeness, we simply clamp the value to [0.0, 1000.0]. | 
|  | 290 | fs << R"__SHADER__( | 
|  | 291 | highp vec3 ToneMap(highp vec3 color) { | 
|  | 292 | return clamp(color, 0.0, 1000.0); | 
|  | 293 | } | 
|  | 294 | )__SHADER__"; | 
|  | 295 | break; | 
|  | 296 | case Key::OUTPUT_TF_ST2084: | 
|  | 297 | fs << R"__SHADER__( | 
|  | 298 | highp vec3 ToneMap(highp vec3 color) { | 
|  | 299 | return color; | 
|  | 300 | } | 
|  | 301 | )__SHADER__"; | 
|  | 302 | break; | 
|  | 303 | default: | 
|  | 304 | fs << R"__SHADER__( | 
|  | 305 | highp vec3 ToneMap(highp vec3 color) { | 
|  | 306 | const float maxMasteringLumi = 1000.0; | 
|  | 307 | const float maxContentLumi = 1000.0; | 
|  | 308 | const float maxInLumi = min(maxMasteringLumi, maxContentLumi); | 
|  | 309 | float maxOutLumi = displayMaxLuminance; | 
|  | 310 |  | 
|  | 311 | float nits = color.y; | 
|  | 312 |  | 
|  | 313 | // clamp to max input luminance | 
|  | 314 | nits = clamp(nits, 0.0, maxInLumi); | 
|  | 315 |  | 
|  | 316 | // scale [0.0, maxInLumi] to [0.0, maxOutLumi] | 
|  | 317 | if (maxInLumi <= maxOutLumi) { | 
| Chia-I Wu | e01fc2c | 2018-10-03 11:32:27 -0700 | [diff] [blame] | 318 | return color * (maxOutLumi / maxInLumi); | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 319 | } else { | 
|  | 320 | // three control points | 
|  | 321 | const float x0 = 10.0; | 
|  | 322 | const float y0 = 17.0; | 
|  | 323 | float x1 = maxOutLumi * 0.75; | 
|  | 324 | float y1 = x1; | 
|  | 325 | float x2 = x1 + (maxInLumi - x1) / 2.0; | 
|  | 326 | float y2 = y1 + (maxOutLumi - y1) * 0.75; | 
|  | 327 |  | 
|  | 328 | // horizontal distances between the last three control points | 
| Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 329 | float h12 = x2 - x1; | 
|  | 330 | float h23 = maxInLumi - x2; | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 331 | // tangents at the last three control points | 
| Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 332 | float m1 = (y2 - y1) / h12; | 
|  | 333 | float m3 = (maxOutLumi - y2) / h23; | 
|  | 334 | float m2 = (m1 + m3) / 2.0; | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 335 |  | 
|  | 336 | if (nits < x0) { | 
|  | 337 | // scale [0.0, x0] to [0.0, y0] linearly | 
| Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 338 | float slope = y0 / x0; | 
| Chia-I Wu | e01fc2c | 2018-10-03 11:32:27 -0700 | [diff] [blame] | 339 | return color * slope; | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 340 | } else if (nits < x1) { | 
|  | 341 | // scale [x0, x1] to [y0, y1] linearly | 
| Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 342 | float slope = (y1 - y0) / (x1 - x0); | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 343 | nits = y0 + (nits - x0) * slope; | 
|  | 344 | } else if (nits < x2) { | 
|  | 345 | // scale [x1, x2] to [y1, y2] using Hermite interp | 
|  | 346 | float t = (nits - x1) / h12; | 
|  | 347 | nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) + | 
|  | 348 | (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t; | 
|  | 349 | } else { | 
|  | 350 | // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp | 
|  | 351 | float t = (nits - x2) / h23; | 
|  | 352 | nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) + | 
|  | 353 | (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t; | 
|  | 354 | } | 
|  | 355 | } | 
|  | 356 |  | 
| Chia-I Wu | e01fc2c | 2018-10-03 11:32:27 -0700 | [diff] [blame] | 357 | // color.y is greater than x0 and is thus non-zero | 
|  | 358 | return color * (nits / color.y); | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 359 | } | 
|  | 360 | )__SHADER__"; | 
|  | 361 | break; | 
|  | 362 | } | 
|  | 363 | break; | 
|  | 364 | default: | 
| Peiyong Lin | 9a1b655 | 2018-05-23 16:52:29 -0700 | [diff] [blame] | 365 | // inverse tone map; the output luminance can be up to maxOutLumi. | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 366 | fs << R"__SHADER__( | 
|  | 367 | highp vec3 ToneMap(highp vec3 color) { | 
| Peiyong Lin | 9a1b655 | 2018-05-23 16:52:29 -0700 | [diff] [blame] | 368 | const float maxOutLumi = 3000.0; | 
|  | 369 |  | 
|  | 370 | const float x0 = 5.0; | 
|  | 371 | const float y0 = 2.5; | 
|  | 372 | float x1 = displayMaxLuminance * 0.7; | 
|  | 373 | float y1 = maxOutLumi * 0.15; | 
|  | 374 | float x2 = displayMaxLuminance * 0.9; | 
|  | 375 | float y2 = maxOutLumi * 0.45; | 
|  | 376 | float x3 = displayMaxLuminance; | 
|  | 377 | float y3 = maxOutLumi; | 
|  | 378 |  | 
|  | 379 | float c1 = y1 / 3.0; | 
|  | 380 | float c2 = y2 / 2.0; | 
|  | 381 | float c3 = y3 / 1.5; | 
|  | 382 |  | 
|  | 383 | float nits = color.y; | 
|  | 384 |  | 
|  | 385 | float scale; | 
|  | 386 | if (nits <= x0) { | 
|  | 387 | // scale [0.0, x0] to [0.0, y0] linearly | 
|  | 388 | const float slope = y0 / x0; | 
| Chia-I Wu | e01fc2c | 2018-10-03 11:32:27 -0700 | [diff] [blame] | 389 | return color * slope; | 
| Peiyong Lin | 9a1b655 | 2018-05-23 16:52:29 -0700 | [diff] [blame] | 390 | } else if (nits <= x1) { | 
|  | 391 | // scale [x0, x1] to [y0, y1] using a curve | 
|  | 392 | float t = (nits - x0) / (x1 - x0); | 
|  | 393 | nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1; | 
|  | 394 | } else if (nits <= x2) { | 
|  | 395 | // scale [x1, x2] to [y1, y2] using a curve | 
|  | 396 | float t = (nits - x1) / (x2 - x1); | 
|  | 397 | nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2; | 
|  | 398 | } else { | 
|  | 399 | // scale [x2, x3] to [y2, y3] using a curve | 
|  | 400 | float t = (nits - x2) / (x3 - x2); | 
|  | 401 | nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3; | 
|  | 402 | } | 
|  | 403 |  | 
| Chia-I Wu | e01fc2c | 2018-10-03 11:32:27 -0700 | [diff] [blame] | 404 | // color.y is greater than x0 and is thus non-zero | 
|  | 405 | return color * (nits / color.y); | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 406 | } | 
|  | 407 | )__SHADER__"; | 
|  | 408 | break; | 
|  | 409 | } | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 410 |  | 
|  | 411 | // convert absolute light to relative light. | 
|  | 412 | switch (needs.getOutputTF()) { | 
|  | 413 | case Key::OUTPUT_TF_ST2084: | 
|  | 414 | fs << R"__SHADER__( | 
|  | 415 | highp vec3 NormalizeLuminance(highp vec3 color) { | 
|  | 416 | return color / 10000.0; | 
|  | 417 | } | 
|  | 418 | )__SHADER__"; | 
|  | 419 | break; | 
|  | 420 | case Key::OUTPUT_TF_HLG: | 
|  | 421 | fs << R"__SHADER__( | 
|  | 422 | highp vec3 NormalizeLuminance(highp vec3 color) { | 
|  | 423 | return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2); | 
|  | 424 | } | 
|  | 425 | )__SHADER__"; | 
|  | 426 | break; | 
|  | 427 | default: | 
|  | 428 | fs << R"__SHADER__( | 
|  | 429 | highp vec3 NormalizeLuminance(highp vec3 color) { | 
|  | 430 | return color / displayMaxLuminance; | 
|  | 431 | } | 
|  | 432 | )__SHADER__"; | 
|  | 433 | break; | 
|  | 434 | } | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | // Generate OOTF that modifies the relative scence light to relative display light. | 
|  | 438 | void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) { | 
|  | 439 | if (!needs.needsToneMapping()) { | 
|  | 440 | fs << R"__SHADER__( | 
|  | 441 | highp vec3 OOTF(const highp vec3 color) { | 
|  | 442 | return color; | 
|  | 443 | } | 
|  | 444 | )__SHADER__"; | 
|  | 445 | } else { | 
|  | 446 | generateToneMappingProcess(fs, needs); | 
|  | 447 | fs << R"__SHADER__( | 
|  | 448 | highp vec3 OOTF(const highp vec3 color) { | 
|  | 449 | return NormalizeLuminance(ToneMap(ScaleLuminance(color))); | 
|  | 450 | } | 
|  | 451 | )__SHADER__"; | 
|  | 452 | } | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 453 | } | 
|  | 454 |  | 
|  | 455 | // Generate OETF that converts relative display light to signal values, | 
|  | 456 | // both normalized to [0, 1] | 
|  | 457 | void ProgramCache::generateOETF(Formatter& fs, const Key& needs) { | 
|  | 458 | switch (needs.getOutputTF()) { | 
|  | 459 | case Key::OUTPUT_TF_SRGB: | 
|  | 460 | fs << R"__SHADER__( | 
|  | 461 | float OETF_sRGB(const float linear) { | 
|  | 462 | return linear <= 0.0031308 ? | 
|  | 463 | linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | vec3 OETF_sRGB(const vec3 linear) { | 
|  | 467 | return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | vec3 OETF(const vec3 linear) { | 
|  | 471 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); | 
|  | 472 | } | 
|  | 473 | )__SHADER__"; | 
|  | 474 | break; | 
|  | 475 | case Key::OUTPUT_TF_ST2084: | 
|  | 476 | fs << R"__SHADER__( | 
|  | 477 | vec3 OETF(const vec3 linear) { | 
| Peiyong Lin | 834be49 | 2018-05-18 15:12:55 -0700 | [diff] [blame] | 478 | const highp float m1 = (2610.0 / 4096.0) / 4.0; | 
|  | 479 | const highp float m2 = (2523.0 / 4096.0) * 128.0; | 
|  | 480 | const highp float c1 = (3424.0 / 4096.0); | 
|  | 481 | const highp float c2 = (2413.0 / 4096.0) * 32.0; | 
|  | 482 | const highp float c3 = (2392.0 / 4096.0) * 32.0; | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 483 |  | 
| Peiyong Lin | 834be49 | 2018-05-18 15:12:55 -0700 | [diff] [blame] | 484 | highp vec3 tmp = pow(linear, vec3(m1)); | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 485 | tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp); | 
|  | 486 | return pow(tmp, vec3(m2)); | 
|  | 487 | } | 
|  | 488 | )__SHADER__"; | 
|  | 489 | break; | 
|  | 490 | case Key::OUTPUT_TF_HLG: | 
|  | 491 | fs << R"__SHADER__( | 
|  | 492 | highp float OETF_channel(const highp float channel) { | 
|  | 493 | const highp float a = 0.17883277; | 
|  | 494 | const highp float b = 0.28466892; | 
|  | 495 | const highp float c = 0.55991073; | 
|  | 496 | return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) : | 
|  | 497 | a * log(12.0 * channel - b) + c; | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | vec3 OETF(const highp vec3 color) { | 
|  | 501 | return vec3(OETF_channel(color.r), OETF_channel(color.g), | 
|  | 502 | OETF_channel(color.b)); | 
|  | 503 | } | 
|  | 504 | )__SHADER__"; | 
|  | 505 | break; | 
|  | 506 | default: | 
|  | 507 | fs << R"__SHADER__( | 
|  | 508 | vec3 OETF(const vec3 linear) { | 
|  | 509 | return linear; | 
|  | 510 | } | 
|  | 511 | )__SHADER__"; | 
|  | 512 | break; | 
|  | 513 | } | 
|  | 514 | } | 
|  | 515 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 516 | String8 ProgramCache::generateVertexShader(const Key& needs) { | 
|  | 517 | Formatter vs; | 
|  | 518 | if (needs.isTexturing()) { | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 519 | vs << "attribute vec4 texCoords;" | 
|  | 520 | << "varying vec2 outTexCoords;"; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 521 | } | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 522 | if (needs.hasRoundedCorners()) { | 
|  | 523 | vs << "attribute lowp vec4 cropCoords;"; | 
|  | 524 | vs << "varying lowp vec2 outCropCoords;"; | 
|  | 525 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 526 | vs << "attribute vec4 position;" | 
|  | 527 | << "uniform mat4 projection;" | 
|  | 528 | << "uniform mat4 texture;" | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 529 | << "void main(void) {" << indent << "gl_Position = projection * position;"; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 530 | if (needs.isTexturing()) { | 
|  | 531 | vs << "outTexCoords = (texture * texCoords).st;"; | 
|  | 532 | } | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 533 | if (needs.hasRoundedCorners()) { | 
|  | 534 | vs << "outCropCoords = cropCoords.st;"; | 
|  | 535 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 536 | vs << dedent << "}"; | 
|  | 537 | return vs.getString(); | 
|  | 538 | } | 
|  | 539 |  | 
|  | 540 | String8 ProgramCache::generateFragmentShader(const Key& needs) { | 
|  | 541 | Formatter fs; | 
|  | 542 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { | 
|  | 543 | fs << "#extension GL_OES_EGL_image_external : require"; | 
|  | 544 | } | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 545 |  | 
|  | 546 | // default precision is required-ish in fragment shaders | 
|  | 547 | fs << "precision mediump float;"; | 
|  | 548 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 549 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { | 
|  | 550 | fs << "uniform samplerExternalOES sampler;" | 
|  | 551 | << "varying vec2 outTexCoords;"; | 
|  | 552 | } else if (needs.getTextureTarget() == Key::TEXTURE_2D) { | 
|  | 553 | fs << "uniform sampler2D sampler;" | 
|  | 554 | << "varying vec2 outTexCoords;"; | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 555 | } | 
|  | 556 |  | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 557 | if (needs.hasRoundedCorners()) { | 
|  | 558 | // Rounded corners implementation using a signed distance function. | 
|  | 559 | fs << R"__SHADER__( | 
|  | 560 | uniform float cornerRadius; | 
|  | 561 | uniform vec2 cropCenter; | 
|  | 562 | varying vec2 outCropCoords; | 
|  | 563 |  | 
|  | 564 | /** | 
|  | 565 | * This function takes the current crop coordinates and calculates an alpha value based | 
|  | 566 | * on the corner radius and distance from the crop center. | 
|  | 567 | */ | 
|  | 568 | float applyCornerRadius(vec2 cropCoords) | 
|  | 569 | { | 
|  | 570 | vec2 position = cropCoords - cropCenter; | 
|  | 571 | vec2 dist = abs(position) + vec2(cornerRadius) - cropCenter; | 
|  | 572 | float plane = length(max(dist, vec2(0.0))); | 
|  | 573 | return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0); | 
|  | 574 | } | 
|  | 575 | )__SHADER__"; | 
|  | 576 | } | 
|  | 577 |  | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 578 | if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 579 | fs << "uniform vec4 color;"; | 
|  | 580 | } | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 581 |  | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 582 | if (needs.isY410BT2020()) { | 
|  | 583 | fs << R"__SHADER__( | 
|  | 584 | vec3 convertY410BT2020(const vec3 color) { | 
|  | 585 | const vec3 offset = vec3(0.0625, 0.5, 0.5); | 
|  | 586 | const mat3 transform = mat3( | 
|  | 587 | vec3(1.1678,  1.1678, 1.1678), | 
|  | 588 | vec3(   0.0, -0.1878, 2.1481), | 
|  | 589 | vec3(1.6836, -0.6523,   0.0)); | 
|  | 590 | // Y is in G, U is in R, and V is in B | 
|  | 591 | return clamp(transform * (color.grb - offset), 0.0, 1.0); | 
|  | 592 | } | 
|  | 593 | )__SHADER__"; | 
|  | 594 | } | 
|  | 595 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 596 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { | 
| Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 597 | // Currently, display maximum luminance is needed when doing tone mapping. | 
|  | 598 | if (needs.needsToneMapping()) { | 
| Chia-I Wu | 8bbacdf | 2018-05-04 15:19:37 -0700 | [diff] [blame] | 599 | fs << "uniform float displayMaxLuminance;"; | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 600 | } | 
| Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 601 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 602 | if (needs.hasInputTransformMatrix()) { | 
| Peiyong Lin | 76dd77a | 2018-05-09 15:35:33 -0700 | [diff] [blame] | 603 | fs << "uniform mat4 inputTransformMatrix;"; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 604 | fs << R"__SHADER__( | 
|  | 605 | highp vec3 InputTransform(const highp vec3 color) { | 
| Chia-I Wu | 3a4f401 | 2018-10-03 11:10:12 -0700 | [diff] [blame] | 606 | return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 607 | } | 
|  | 608 | )__SHADER__"; | 
|  | 609 | } else { | 
|  | 610 | fs << R"__SHADER__( | 
|  | 611 | highp vec3 InputTransform(const highp vec3 color) { | 
|  | 612 | return color; | 
|  | 613 | } | 
|  | 614 | )__SHADER__"; | 
|  | 615 | } | 
|  | 616 |  | 
|  | 617 | // the transformation from a wider colorspace to a narrower one can | 
|  | 618 | // result in >1.0 or <0.0 pixel values | 
|  | 619 | if (needs.hasOutputTransformMatrix()) { | 
|  | 620 | fs << "uniform mat4 outputTransformMatrix;"; | 
|  | 621 | fs << R"__SHADER__( | 
|  | 622 | highp vec3 OutputTransform(const highp vec3 color) { | 
|  | 623 | return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0); | 
|  | 624 | } | 
|  | 625 | )__SHADER__"; | 
|  | 626 | } else { | 
|  | 627 | fs << R"__SHADER__( | 
|  | 628 | highp vec3 OutputTransform(const highp vec3 color) { | 
|  | 629 | return clamp(color, 0.0, 1.0); | 
|  | 630 | } | 
|  | 631 | )__SHADER__"; | 
|  | 632 | } | 
|  | 633 |  | 
| Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 634 | generateEOTF(fs, needs); | 
|  | 635 | generateOOTF(fs, needs); | 
|  | 636 | generateOETF(fs, needs); | 
| Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 637 | } | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 638 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 639 | fs << "void main(void) {" << indent; | 
|  | 640 | if (needs.isTexturing()) { | 
|  | 641 | fs << "gl_FragColor = texture2D(sampler, outTexCoords);"; | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 642 | if (needs.isY410BT2020()) { | 
|  | 643 | fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);"; | 
|  | 644 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 645 | } else { | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 646 | fs << "gl_FragColor.rgb = color.rgb;"; | 
|  | 647 | fs << "gl_FragColor.a = 1.0;"; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 648 | } | 
| Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 649 | if (needs.isOpaque()) { | 
|  | 650 | fs << "gl_FragColor.a = 1.0;"; | 
|  | 651 | } | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 652 | if (needs.hasAlpha()) { | 
|  | 653 | // modulate the current alpha value with alpha set | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 654 | if (needs.isPremultiplied()) { | 
|  | 655 | // ... and the color too if we're premultiplied | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 656 | fs << "gl_FragColor *= color.a;"; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 657 | } else { | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 658 | fs << "gl_FragColor.a *= color.a;"; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 659 | } | 
|  | 660 | } | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 661 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 662 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 663 | if (!needs.isOpaque() && needs.isPremultiplied()) { | 
|  | 664 | // un-premultiply if needed before linearization | 
| Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 665 | // avoid divide by 0 by adding 0.5/256 to the alpha channel | 
|  | 666 | fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);"; | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 667 | } | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 668 | fs << "gl_FragColor.rgb = " | 
|  | 669 | "OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));"; | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 670 | if (!needs.isOpaque() && needs.isPremultiplied()) { | 
|  | 671 | // and re-premultiply if needed after gamma correction | 
| Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 672 | fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);"; | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 673 | } | 
|  | 674 | } | 
|  | 675 |  | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 676 | if (needs.hasRoundedCorners()) { | 
|  | 677 | if (needs.isPremultiplied()) { | 
|  | 678 | fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));"; | 
|  | 679 | } else { | 
|  | 680 | fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);"; | 
|  | 681 | } | 
|  | 682 | } | 
|  | 683 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 684 | fs << dedent << "}"; | 
|  | 685 | return fs.getString(); | 
|  | 686 | } | 
|  | 687 |  | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 688 | std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) { | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 689 | ATRACE_CALL(); | 
|  | 690 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 691 | // vertex shader | 
|  | 692 | String8 vs = generateVertexShader(needs); | 
|  | 693 |  | 
|  | 694 | // fragment shader | 
|  | 695 | String8 fs = generateFragmentShader(needs); | 
|  | 696 |  | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 697 | return std::make_unique<Program>(needs, vs.string(), fs.string()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 698 | } | 
|  | 699 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 700 | void ProgramCache::useProgram(EGLContext context, const Description& description) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 701 | // generate the key for the shader based on the description | 
|  | 702 | Key needs(computeKey(description)); | 
|  | 703 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 704 | // look-up the program in the cache | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 705 | auto& cache = mCaches[context]; | 
|  | 706 | auto it = cache.find(needs); | 
|  | 707 | if (it == cache.end()) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 708 | // we didn't find our program, so generate one... | 
| Chia-I Wu | 56d7b0a | 2018-10-01 15:13:11 -0700 | [diff] [blame] | 709 | nsecs_t time = systemTime(); | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 710 | it = cache.emplace(needs, generateProgram(needs)).first; | 
| Chia-I Wu | 56d7b0a | 2018-10-01 15:13:11 -0700 | [diff] [blame] | 711 | time = systemTime() - time; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 712 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 713 | ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)", | 
|  | 714 | context, needs.mKey, uint32_t(ns2ms(time)), cache.size()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 715 | } | 
|  | 716 |  | 
|  | 717 | // here we have a suitable program for this description | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 718 | std::unique_ptr<Program>& program = it->second; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 719 | if (program->isValid()) { | 
|  | 720 | program->use(); | 
|  | 721 | program->setUniforms(description); | 
|  | 722 | } | 
|  | 723 | } | 
|  | 724 |  | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 725 | } // namespace gl | 
|  | 726 | } // namespace renderengine | 
|  | 727 | } // namespace android |