blob: 96ccf5c512f3e0124f0ca0a7256c8a0b2ded9fec [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
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 Wu93e14df2018-06-04 10:10:17 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Peiyong Lin833074a2018-08-28 11:53:54 -070019#include "ProgramCache.h"
Peiyong Lincbc184f2018-08-22 13:24:10 -070020
Mathias Agopian3f844832013-08-07 21:24:32 -070021#include <GLES2/gl2.h>
22#include <GLES2/gl2ext.h>
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070023#include <log/log.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070024#include <renderengine/private/Description.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070025#include <utils/String8.h>
Chia-I Wu93e14df2018-06-04 10:10:17 -070026#include <utils/Trace.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070027#include "Program.h"
28
29ANDROID_SINGLETON_STATIC_INSTANCE(android::renderengine::gl::ProgramCache)
Mathias Agopian3f844832013-08-07 21:24:32 -070030
Mathias Agopian3f844832013-08-07 21:24:32 -070031namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070032namespace renderengine {
33namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -070034
Mathias Agopian3f844832013-08-07 21:24:32 -070035/*
36 * A simple formatter class to automatically add the endl and
37 * manage the indentation.
38 */
39
40class Formatter;
41static Formatter& indent(Formatter& f);
42static Formatter& dedent(Formatter& f);
43
44class 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 Wub027f802017-11-29 14:00:52 -080050
Mathias Agopian3f844832013-08-07 21:24:32 -070051public:
Andy McFadden892f22d2013-08-15 10:05:01 -070052 Formatter() : mIndent(0) {}
53
Chia-I Wub027f802017-11-29 14:00:52 -080054 String8 getString() const { return mString; }
Mathias Agopian3f844832013-08-07 21:24:32 -070055
Chia-I Wub027f802017-11-29 14:00:52 -080056 friend Formatter& operator<<(Formatter& out, const char* in) {
57 for (int i = 0; i < out.mIndent; i++) {
Mathias Agopian3f844832013-08-07 21:24:32 -070058 out.mString.append(" ");
59 }
60 out.mString.append(in);
61 out.mString.append("\n");
62 return out;
63 }
Chia-I Wub027f802017-11-29 14:00:52 -080064 friend inline Formatter& operator<<(Formatter& out, const String8& in) {
65 return operator<<(out, in.string());
Mathias Agopian3f844832013-08-07 21:24:32 -070066 }
67 friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) {
68 return (*func)(to);
69 }
70};
71Formatter& indent(Formatter& f) {
72 f.mIndent++;
73 return f;
74}
75Formatter& dedent(Formatter& f) {
76 f.mIndent--;
77 return f;
78}
79
Alec Mouri47bcb072023-08-15 02:02:49 +000080void ProgramCache::primeCache(EGLContext context, bool toneMapperShaderOnly) {
Peiyong Linfb530cf2018-12-15 05:07:38 +000081 auto& cache = mCaches[context];
Riley Andrewsa51fafc2014-09-29 13:29:40 -070082 uint32_t shaderCount = 0;
Chong Zhang8ddb04e2019-10-02 14:20:02 -070083
84 if (toneMapperShaderOnly) {
85 Key shaderKey;
86 // base settings used by HDR->SDR tonemap only
87 shaderKey.set(Key::BLEND_MASK | Key::INPUT_TRANSFORM_MATRIX_MASK |
88 Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::OUTPUT_TF_MASK |
89 Key::OPACITY_MASK | Key::ALPHA_MASK |
90 Key::ROUNDED_CORNERS_MASK | Key::TEXTURE_MASK,
91 Key::BLEND_NORMAL | Key::INPUT_TRANSFORM_MATRIX_ON |
92 Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::OUTPUT_TF_SRGB |
93 Key::OPACITY_OPAQUE | Key::ALPHA_EQ_ONE |
94 Key::ROUNDED_CORNERS_OFF | Key::TEXTURE_EXT);
95 for (int i = 0; i < 4; i++) {
96 // Cache input transfer for HLG & ST2084
97 shaderKey.set(Key::INPUT_TF_MASK, (i & 1) ?
98 Key::INPUT_TF_HLG : Key::INPUT_TF_ST2084);
99
Chong Zhang8ddb04e2019-10-02 14:20:02 -0700100 if (cache.count(shaderKey) == 0) {
101 cache.emplace(shaderKey, generateProgram(shaderKey));
102 shaderCount++;
103 }
104 }
105 return;
106 }
107
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700108 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK
109 | Key::ROUNDED_CORNERS_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700110 // Prime the cache for all combinations of the above masks,
111 // leaving off the experimental color matrix mask options.
112
113 nsecs_t timeBefore = systemTime();
114 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
115 Key shaderKey;
116 shaderKey.set(keyMask, keyVal);
117 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -0800118 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700119 continue;
120 }
Peiyong Linfb530cf2018-12-15 05:07:38 +0000121 if (cache.count(shaderKey) == 0) {
122 cache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700123 shaderCount++;
124 }
125 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700126
127 // Prime for sRGB->P3 conversion
Alec Mouri47bcb072023-08-15 02:02:49 +0000128 Key shaderKey;
129 shaderKey.set(Key::BLEND_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::INPUT_TF_MASK |
130 Key::OUTPUT_TF_MASK,
131 Key::BLEND_PREMULT | Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::INPUT_TF_SRGB |
132 Key::OUTPUT_TF_SRGB);
133 for (int i = 0; i < 16; i++) {
134 shaderKey.set(Key::OPACITY_MASK, (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
135 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Peiyong Linc1bb20c2019-03-25 11:06:12 -0700136
Alec Mouri47bcb072023-08-15 02:02:49 +0000137 // Cache rounded corners
138 shaderKey.set(Key::ROUNDED_CORNERS_MASK,
139 (i & 4) ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
Peiyong Linc1bb20c2019-03-25 11:06:12 -0700140
Alec Mouri47bcb072023-08-15 02:02:49 +0000141 // Cache texture off option for window transition
142 shaderKey.set(Key::TEXTURE_MASK, (i & 8) ? Key::TEXTURE_EXT : Key::TEXTURE_OFF);
143 if (cache.count(shaderKey) == 0) {
144 cache.emplace(shaderKey, generateProgram(shaderKey));
145 shaderCount++;
Chia-I Wu93e14df2018-06-04 10:10:17 -0700146 }
147 }
148
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700149 nsecs_t timeAfter = systemTime();
150 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
151 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
152}
153
Mathias Agopian3f844832013-08-07 21:24:32 -0700154ProgramCache::Key ProgramCache::computeKey(const Description& description) {
155 Key needs;
156 needs.set(Key::TEXTURE_MASK,
Alec Mouri994761f2023-08-04 21:50:55 +0000157 !description.textureEnabled ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700158 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Alec Mouri994761f2023-08-04 21:50:55 +0000159 ? Key::TEXTURE_EXT
160 : description.texture.getTextureTarget() == GL_TEXTURE_2D ? Key::TEXTURE_2D
161 : Key::TEXTURE_OFF)
Peiyong Lin46080ef2018-10-26 18:43:14 -0700162 .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800163 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700164 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800165 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700166 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700167 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800168 description.hasInputTransformMatrix() ? Key::INPUT_TRANSFORM_MATRIX_ON
169 : Key::INPUT_TRANSFORM_MATRIX_OFF)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700170 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Peiyong Lin46080ef2018-10-26 18:43:14 -0700171 description.hasOutputTransformMatrix() || description.hasColorMatrix()
172 ? Key::OUTPUT_TRANSFORM_MATRIX_ON
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700173 : Key::OUTPUT_TRANSFORM_MATRIX_OFF)
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800174 .set(Key::Key::DISPLAY_COLOR_TRANSFORM_MATRIX_MASK,
175 description.hasDisplayColorMatrix() ? Key::DISPLAY_COLOR_TRANSFORM_MATRIX_ON
176 : Key::DISPLAY_COLOR_TRANSFORM_MATRIX_OFF)
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700177 .set(Key::ROUNDED_CORNERS_MASK,
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800178 description.cornerRadius > 0 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF)
179 .set(Key::SHADOW_MASK, description.drawShadows ? Key::SHADOW_ON : Key::SHADOW_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800180
Peiyong Lin646f9342018-12-27 15:24:25 -0800181 if (needs.hasTransformMatrix() ||
182 (description.inputTransferFunction != description.outputTransferFunction)) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700183 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800184 case Description::TransferFunction::LINEAR:
185 default:
186 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
187 break;
188 case Description::TransferFunction::SRGB:
189 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
190 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800191 case Description::TransferFunction::ST2084:
192 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
193 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700194 case Description::TransferFunction::HLG:
195 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
196 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800197 }
198
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700199 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800200 case Description::TransferFunction::LINEAR:
201 default:
202 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
203 break;
204 case Description::TransferFunction::SRGB:
205 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
206 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800207 case Description::TransferFunction::ST2084:
208 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
209 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700210 case Description::TransferFunction::HLG:
211 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
212 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800213 }
214 }
215
Mathias Agopian3f844832013-08-07 21:24:32 -0700216 return needs;
217}
218
Peiyong Lin2542cb02018-04-30 17:29:53 -0700219// Generate EOTF that converts signal values to relative display light,
220// both normalized to [0, 1].
221void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
222 switch (needs.getInputTF()) {
223 case Key::INPUT_TF_SRGB:
224 fs << R"__SHADER__(
225 float EOTF_sRGB(float srgb) {
226 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
227 }
228
229 vec3 EOTF_sRGB(const vec3 srgb) {
230 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
231 }
232
233 vec3 EOTF(const vec3 srgb) {
234 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
235 }
236 )__SHADER__";
237 break;
238 case Key::INPUT_TF_ST2084:
239 fs << R"__SHADER__(
240 vec3 EOTF(const highp vec3 color) {
241 const highp float m1 = (2610.0 / 4096.0) / 4.0;
242 const highp float m2 = (2523.0 / 4096.0) * 128.0;
243 const highp float c1 = (3424.0 / 4096.0);
244 const highp float c2 = (2413.0 / 4096.0) * 32.0;
245 const highp float c3 = (2392.0 / 4096.0) * 32.0;
246
Siddharth Kapoorac1f7c92018-12-05 20:29:06 +0800247 highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700248 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
249 return pow(tmp, 1.0 / vec3(m1));
250 }
251 )__SHADER__";
252 break;
253 case Key::INPUT_TF_HLG:
254 fs << R"__SHADER__(
255 highp float EOTF_channel(const highp float channel) {
256 const highp float a = 0.17883277;
257 const highp float b = 0.28466892;
258 const highp float c = 0.55991073;
259 return channel <= 0.5 ? channel * channel / 3.0 :
260 (exp((channel - c) / a) + b) / 12.0;
261 }
262
263 vec3 EOTF(const highp vec3 color) {
264 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
265 EOTF_channel(color.b));
266 }
267 )__SHADER__";
268 break;
269 default:
270 fs << R"__SHADER__(
271 vec3 EOTF(const vec3 linear) {
272 return linear;
273 }
274 )__SHADER__";
275 break;
276 }
277}
278
Peiyong Lin53f320e2018-04-23 17:31:06 -0700279void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
280 // Convert relative light to absolute light.
281 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700282 case Key::INPUT_TF_ST2084:
283 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700284 highp vec3 ScaleLuminance(highp vec3 color) {
285 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700286 }
287 )__SHADER__";
288 break;
289 case Key::INPUT_TF_HLG:
290 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700291 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700292 // The formula is:
293 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700294 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
295 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700296 }
297 )__SHADER__";
298 break;
299 default:
300 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700301 highp vec3 ScaleLuminance(highp vec3 color) {
302 return color * displayMaxLuminance;
303 }
304 )__SHADER__";
305 break;
306 }
307
308 // Tone map absolute light to display luminance range.
309 switch (needs.getInputTF()) {
310 case Key::INPUT_TF_ST2084:
311 case Key::INPUT_TF_HLG:
312 switch (needs.getOutputTF()) {
313 case Key::OUTPUT_TF_HLG:
314 // Right now when mixed PQ and HLG contents are presented,
315 // HLG content will always be converted to PQ. However, for
316 // completeness, we simply clamp the value to [0.0, 1000.0].
317 fs << R"__SHADER__(
318 highp vec3 ToneMap(highp vec3 color) {
319 return clamp(color, 0.0, 1000.0);
320 }
321 )__SHADER__";
322 break;
323 case Key::OUTPUT_TF_ST2084:
324 fs << R"__SHADER__(
325 highp vec3 ToneMap(highp vec3 color) {
326 return color;
327 }
328 )__SHADER__";
329 break;
330 default:
331 fs << R"__SHADER__(
332 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800333 float maxMasteringLumi = maxMasteringLuminance;
334 float maxContentLumi = maxContentLuminance;
335 float maxInLumi = min(maxMasteringLumi, maxContentLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700336 float maxOutLumi = displayMaxLuminance;
337
338 float nits = color.y;
339
340 // clamp to max input luminance
341 nits = clamp(nits, 0.0, maxInLumi);
342
343 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
344 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700345 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700346 } else {
347 // three control points
348 const float x0 = 10.0;
349 const float y0 = 17.0;
350 float x1 = maxOutLumi * 0.75;
351 float y1 = x1;
352 float x2 = x1 + (maxInLumi - x1) / 2.0;
353 float y2 = y1 + (maxOutLumi - y1) * 0.75;
354
355 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700356 float h12 = x2 - x1;
357 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700358 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700359 float m1 = (y2 - y1) / h12;
360 float m3 = (maxOutLumi - y2) / h23;
361 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700362
363 if (nits < x0) {
364 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700365 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700366 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700367 } else if (nits < x1) {
368 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700369 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700370 nits = y0 + (nits - x0) * slope;
371 } else if (nits < x2) {
372 // scale [x1, x2] to [y1, y2] using Hermite interp
373 float t = (nits - x1) / h12;
374 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
375 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
376 } else {
377 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
378 float t = (nits - x2) / h23;
379 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
380 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
381 }
382 }
383
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700384 // color.y is greater than x0 and is thus non-zero
385 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700386 }
387 )__SHADER__";
388 break;
389 }
390 break;
391 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700392 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700393 fs << R"__SHADER__(
394 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700395 const float maxOutLumi = 3000.0;
396
397 const float x0 = 5.0;
398 const float y0 = 2.5;
399 float x1 = displayMaxLuminance * 0.7;
400 float y1 = maxOutLumi * 0.15;
401 float x2 = displayMaxLuminance * 0.9;
402 float y2 = maxOutLumi * 0.45;
403 float x3 = displayMaxLuminance;
404 float y3 = maxOutLumi;
405
406 float c1 = y1 / 3.0;
407 float c2 = y2 / 2.0;
408 float c3 = y3 / 1.5;
409
410 float nits = color.y;
411
412 float scale;
413 if (nits <= x0) {
414 // scale [0.0, x0] to [0.0, y0] linearly
415 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700416 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700417 } else if (nits <= x1) {
418 // scale [x0, x1] to [y0, y1] using a curve
419 float t = (nits - x0) / (x1 - x0);
420 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
421 } else if (nits <= x2) {
422 // scale [x1, x2] to [y1, y2] using a curve
423 float t = (nits - x1) / (x2 - x1);
424 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
425 } else {
426 // scale [x2, x3] to [y2, y3] using a curve
427 float t = (nits - x2) / (x3 - x2);
428 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
429 }
430
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700431 // color.y is greater than x0 and is thus non-zero
432 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700433 }
434 )__SHADER__";
435 break;
436 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700437
438 // convert absolute light to relative light.
439 switch (needs.getOutputTF()) {
440 case Key::OUTPUT_TF_ST2084:
441 fs << R"__SHADER__(
442 highp vec3 NormalizeLuminance(highp vec3 color) {
443 return color / 10000.0;
444 }
445 )__SHADER__";
446 break;
447 case Key::OUTPUT_TF_HLG:
448 fs << R"__SHADER__(
449 highp vec3 NormalizeLuminance(highp vec3 color) {
450 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
451 }
452 )__SHADER__";
453 break;
454 default:
455 fs << R"__SHADER__(
456 highp vec3 NormalizeLuminance(highp vec3 color) {
457 return color / displayMaxLuminance;
458 }
459 )__SHADER__";
460 break;
461 }
462}
463
464// Generate OOTF that modifies the relative scence light to relative display light.
465void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
466 if (!needs.needsToneMapping()) {
467 fs << R"__SHADER__(
468 highp vec3 OOTF(const highp vec3 color) {
469 return color;
470 }
471 )__SHADER__";
472 } else {
473 generateToneMappingProcess(fs, needs);
474 fs << R"__SHADER__(
475 highp vec3 OOTF(const highp vec3 color) {
476 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
477 }
478 )__SHADER__";
479 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700480}
481
482// Generate OETF that converts relative display light to signal values,
483// both normalized to [0, 1]
484void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
485 switch (needs.getOutputTF()) {
486 case Key::OUTPUT_TF_SRGB:
487 fs << R"__SHADER__(
488 float OETF_sRGB(const float linear) {
489 return linear <= 0.0031308 ?
490 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
491 }
492
493 vec3 OETF_sRGB(const vec3 linear) {
494 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
495 }
496
497 vec3 OETF(const vec3 linear) {
498 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
499 }
500 )__SHADER__";
501 break;
502 case Key::OUTPUT_TF_ST2084:
503 fs << R"__SHADER__(
504 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700505 const highp float m1 = (2610.0 / 4096.0) / 4.0;
506 const highp float m2 = (2523.0 / 4096.0) * 128.0;
507 const highp float c1 = (3424.0 / 4096.0);
508 const highp float c2 = (2413.0 / 4096.0) * 32.0;
509 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700510
Peiyong Lin834be492018-05-18 15:12:55 -0700511 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700512 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
513 return pow(tmp, vec3(m2));
514 }
515 )__SHADER__";
516 break;
517 case Key::OUTPUT_TF_HLG:
518 fs << R"__SHADER__(
519 highp float OETF_channel(const highp float channel) {
520 const highp float a = 0.17883277;
521 const highp float b = 0.28466892;
522 const highp float c = 0.55991073;
523 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
524 a * log(12.0 * channel - b) + c;
525 }
526
527 vec3 OETF(const highp vec3 color) {
528 return vec3(OETF_channel(color.r), OETF_channel(color.g),
529 OETF_channel(color.b));
530 }
531 )__SHADER__";
532 break;
533 default:
534 fs << R"__SHADER__(
535 vec3 OETF(const vec3 linear) {
536 return linear;
537 }
538 )__SHADER__";
539 break;
540 }
541}
542
Mathias Agopian3f844832013-08-07 21:24:32 -0700543String8 ProgramCache::generateVertexShader(const Key& needs) {
544 Formatter vs;
Vishnu Nairf19544f2020-02-03 11:23:26 -0800545 if (needs.hasTextureCoords()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800546 vs << "attribute vec4 texCoords;"
547 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700548 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700549 if (needs.hasRoundedCorners()) {
550 vs << "attribute lowp vec4 cropCoords;";
551 vs << "varying lowp vec2 outCropCoords;";
552 }
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800553 if (needs.drawShadows()) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800554 vs << "attribute lowp vec4 shadowColor;";
555 vs << "varying lowp vec4 outShadowColor;";
556 vs << "attribute lowp vec4 shadowParams;";
557 vs << "varying lowp vec3 outShadowParams;";
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800558 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700559 vs << "attribute vec4 position;"
560 << "uniform mat4 projection;"
561 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800562 << "void main(void) {" << indent << "gl_Position = projection * position;";
Vishnu Nairf19544f2020-02-03 11:23:26 -0800563 if (needs.hasTextureCoords()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700564 vs << "outTexCoords = (texture * texCoords).st;";
565 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700566 if (needs.hasRoundedCorners()) {
567 vs << "outCropCoords = cropCoords.st;";
568 }
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800569 if (needs.drawShadows()) {
570 vs << "outShadowColor = shadowColor;";
571 vs << "outShadowParams = shadowParams.xyz;";
572 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700573 vs << dedent << "}";
574 return vs.getString();
575}
576
577String8 ProgramCache::generateFragmentShader(const Key& needs) {
578 Formatter fs;
579 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
580 fs << "#extension GL_OES_EGL_image_external : require";
581 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700582
583 // default precision is required-ish in fragment shaders
584 fs << "precision mediump float;";
585
Mathias Agopian3f844832013-08-07 21:24:32 -0700586 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800587 fs << "uniform samplerExternalOES sampler;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700588 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800589 fs << "uniform sampler2D sampler;";
590 }
591
592 if (needs.hasTextureCoords()) {
Chia-I Wu82bafb32022-10-19 14:41:59 -0700593 fs << "varying highp vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700594 }
595
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700596 if (needs.hasRoundedCorners()) {
597 // Rounded corners implementation using a signed distance function.
598 fs << R"__SHADER__(
599 uniform float cornerRadius;
600 uniform vec2 cropCenter;
601 varying vec2 outCropCoords;
602
603 /**
604 * This function takes the current crop coordinates and calculates an alpha value based
605 * on the corner radius and distance from the crop center.
606 */
607 float applyCornerRadius(vec2 cropCoords)
608 {
609 vec2 position = cropCoords - cropCenter;
Alec Mouri0d0e16e2019-07-25 15:22:29 -0700610 // Scale down the dist vector here, as otherwise large corner
611 // radii can cause floating point issues when computing the norm
612 vec2 dist = (abs(position) - cropCenter + vec2(cornerRadius)) / 16.0;
613 // Once we've found the norm, then scale back up.
614 float plane = length(max(dist, vec2(0.0))) * 16.0;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700615 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
616 }
617 )__SHADER__";
618 }
619
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800620 if (needs.drawShadows()) {
621 fs << R"__SHADER__(
Vishnu Nairf19544f2020-02-03 11:23:26 -0800622 varying lowp vec4 outShadowColor;
623 varying lowp vec3 outShadowParams;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800624
625 /**
626 * Returns the shadow color.
627 */
628 vec4 getShadowColor()
629 {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800630 lowp float d = length(outShadowParams.xy);
631 vec2 uv = vec2(outShadowParams.z * (1.0 - d), 0.5);
632 lowp float factor = texture2D(sampler, uv).a;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800633 return outShadowColor * factor;
634 }
635 )__SHADER__";
636 }
637
chaviw13fdc492017-06-27 12:40:18 -0700638 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700639 fs << "uniform vec4 color;";
640 }
chaviw13fdc492017-06-27 12:40:18 -0700641
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800642 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF()) ||
643 needs.hasDisplayColorMatrix()) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700644 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700645 fs << "uniform float displayMaxLuminance;";
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800646 fs << "uniform float maxMasteringLuminance;";
647 fs << "uniform float maxContentLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700648 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700649
Peiyong Lina296b0c2018-04-30 16:55:29 -0700650 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700651 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700652 fs << R"__SHADER__(
653 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700654 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700655 }
656 )__SHADER__";
657 } else {
658 fs << R"__SHADER__(
659 highp vec3 InputTransform(const highp vec3 color) {
660 return color;
661 }
662 )__SHADER__";
663 }
664
665 // the transformation from a wider colorspace to a narrower one can
666 // result in >1.0 or <0.0 pixel values
667 if (needs.hasOutputTransformMatrix()) {
668 fs << "uniform mat4 outputTransformMatrix;";
669 fs << R"__SHADER__(
670 highp vec3 OutputTransform(const highp vec3 color) {
671 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
672 }
673 )__SHADER__";
674 } else {
675 fs << R"__SHADER__(
676 highp vec3 OutputTransform(const highp vec3 color) {
677 return clamp(color, 0.0, 1.0);
678 }
679 )__SHADER__";
680 }
681
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800682 if (needs.hasDisplayColorMatrix()) {
683 fs << "uniform mat4 displayColorMatrix;";
684 fs << R"__SHADER__(
685 highp vec3 DisplayColorMatrix(const highp vec3 color) {
686 return clamp(vec3(displayColorMatrix * vec4(color, 1.0)), 0.0, 1.0);
687 }
688 )__SHADER__";
689 } else {
690 fs << R"__SHADER__(
691 highp vec3 DisplayColorMatrix(const highp vec3 color) {
692 return color;
693 }
694 )__SHADER__";
695 }
696
Peiyong Lin2542cb02018-04-30 17:29:53 -0700697 generateEOTF(fs, needs);
698 generateOOTF(fs, needs);
699 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700700 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800701
Mathias Agopian3f844832013-08-07 21:24:32 -0700702 fs << "void main(void) {" << indent;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800703 if (needs.drawShadows()) {
704 fs << "gl_FragColor = getShadowColor();";
Mathias Agopian3f844832013-08-07 21:24:32 -0700705 } else {
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800706 if (needs.isTexturing()) {
707 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Mathias Agopian3f844832013-08-07 21:24:32 -0700708 } else {
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800709 fs << "gl_FragColor.rgb = color.rgb;";
710 fs << "gl_FragColor.a = 1.0;";
711 }
712 if (needs.isOpaque()) {
713 fs << "gl_FragColor.a = 1.0;";
714 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700715 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700716
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800717 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF()) ||
718 needs.hasDisplayColorMatrix()) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700719 if (!needs.isOpaque() && needs.isPremultiplied()) {
720 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700721 // avoid divide by 0 by adding 0.5/256 to the alpha channel
722 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700723 }
Peiyong Lin46080ef2018-10-26 18:43:14 -0700724 fs << "gl_FragColor.rgb = "
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800725 "DisplayColorMatrix(OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))"
726 ")));";
727
Mathias Agopianff2ed702013-09-01 21:36:12 -0700728 if (!needs.isOpaque() && needs.isPremultiplied()) {
729 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700730 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700731 }
732 }
733
KaiChieh Chuangda2845c2020-12-14 16:49:38 +0800734 /*
735 * Whether applying layer alpha before or after color transform doesn't matter,
736 * as long as we can undo premultiplication. But we cannot un-premultiply
737 * for color transform if the layer alpha = 0, e.g. 0 / (0 + 0.0019) = 0.
738 */
739 if (!needs.drawShadows()) {
740 if (needs.hasAlpha()) {
741 // modulate the current alpha value with alpha set
742 if (needs.isPremultiplied()) {
743 // ... and the color too if we're premultiplied
744 fs << "gl_FragColor *= color.a;";
745 } else {
746 fs << "gl_FragColor.a *= color.a;";
747 }
748 }
749 }
750
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700751 if (needs.hasRoundedCorners()) {
752 if (needs.isPremultiplied()) {
753 fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
754 } else {
755 fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
756 }
757 }
758
Mathias Agopian3f844832013-08-07 21:24:32 -0700759 fs << dedent << "}";
760 return fs.getString();
761}
762
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700763std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700764 ATRACE_CALL();
765
Mathias Agopian3f844832013-08-07 21:24:32 -0700766 // vertex shader
767 String8 vs = generateVertexShader(needs);
768
769 // fragment shader
770 String8 fs = generateFragmentShader(needs);
771
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700772 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700773}
774
Peiyong Linfb530cf2018-12-15 05:07:38 +0000775void ProgramCache::useProgram(EGLContext context, const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700776 // generate the key for the shader based on the description
777 Key needs(computeKey(description));
778
Chia-I Wub027f802017-11-29 14:00:52 -0800779 // look-up the program in the cache
Peiyong Linfb530cf2018-12-15 05:07:38 +0000780 auto& cache = mCaches[context];
781 auto it = cache.find(needs);
782 if (it == cache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700783 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700784 nsecs_t time = systemTime();
Peiyong Linfb530cf2018-12-15 05:07:38 +0000785 it = cache.emplace(needs, generateProgram(needs)).first;
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700786 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700787
Peiyong Linfb530cf2018-12-15 05:07:38 +0000788 ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)",
789 context, needs.mKey, uint32_t(ns2ms(time)), cache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700790 }
791
792 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700793 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700794 if (program->isValid()) {
795 program->use();
796 program->setUniforms(description);
797 }
798}
799
Peiyong Lin46080ef2018-10-26 18:43:14 -0700800} // namespace gl
801} // namespace renderengine
802} // namespace android