blob: 812dda04fa1029d3cd69047840e99ce6cf1e2fc5 [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
Chong Zhang8ddb04e2019-10-02 14:20:02 -070080void ProgramCache::primeCache(
81 EGLContext context, bool useColorManagement, bool toneMapperShaderOnly) {
Peiyong Linfb530cf2018-12-15 05:07:38 +000082 auto& cache = mCaches[context];
Riley Andrewsa51fafc2014-09-29 13:29:40 -070083 uint32_t shaderCount = 0;
Chong Zhang8ddb04e2019-10-02 14:20:02 -070084
85 if (toneMapperShaderOnly) {
86 Key shaderKey;
87 // base settings used by HDR->SDR tonemap only
88 shaderKey.set(Key::BLEND_MASK | Key::INPUT_TRANSFORM_MATRIX_MASK |
89 Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::OUTPUT_TF_MASK |
90 Key::OPACITY_MASK | Key::ALPHA_MASK |
91 Key::ROUNDED_CORNERS_MASK | Key::TEXTURE_MASK,
92 Key::BLEND_NORMAL | Key::INPUT_TRANSFORM_MATRIX_ON |
93 Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::OUTPUT_TF_SRGB |
94 Key::OPACITY_OPAQUE | Key::ALPHA_EQ_ONE |
95 Key::ROUNDED_CORNERS_OFF | Key::TEXTURE_EXT);
96 for (int i = 0; i < 4; i++) {
97 // Cache input transfer for HLG & ST2084
98 shaderKey.set(Key::INPUT_TF_MASK, (i & 1) ?
99 Key::INPUT_TF_HLG : Key::INPUT_TF_ST2084);
100
Chong Zhang8ddb04e2019-10-02 14:20:02 -0700101 if (cache.count(shaderKey) == 0) {
102 cache.emplace(shaderKey, generateProgram(shaderKey));
103 shaderCount++;
104 }
105 }
106 return;
107 }
108
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700109 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK
110 | Key::ROUNDED_CORNERS_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700111 // Prime the cache for all combinations of the above masks,
112 // leaving off the experimental color matrix mask options.
113
114 nsecs_t timeBefore = systemTime();
115 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
116 Key shaderKey;
117 shaderKey.set(keyMask, keyVal);
118 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -0800119 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700120 continue;
121 }
Peiyong Linfb530cf2018-12-15 05:07:38 +0000122 if (cache.count(shaderKey) == 0) {
123 cache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700124 shaderCount++;
125 }
126 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700127
128 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700129 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700130 Key shaderKey;
Peiyong Linc1bb20c2019-03-25 11:06:12 -0700131 shaderKey.set(Key::BLEND_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::INPUT_TF_MASK |
132 Key::OUTPUT_TF_MASK,
133 Key::BLEND_PREMULT | Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::INPUT_TF_SRGB |
134 Key::OUTPUT_TF_SRGB);
135 for (int i = 0; i < 16; i++) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700136 shaderKey.set(Key::OPACITY_MASK,
137 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
138 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Peiyong Linc1bb20c2019-03-25 11:06:12 -0700139
140 // Cache rounded corners
141 shaderKey.set(Key::ROUNDED_CORNERS_MASK,
142 (i & 4) ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
143
144 // Cache texture off option for window transition
145 shaderKey.set(Key::TEXTURE_MASK, (i & 8) ? Key::TEXTURE_EXT : Key::TEXTURE_OFF);
Peiyong Linfb530cf2018-12-15 05:07:38 +0000146 if (cache.count(shaderKey) == 0) {
147 cache.emplace(shaderKey, generateProgram(shaderKey));
Chia-I Wu93e14df2018-06-04 10:10:17 -0700148 shaderCount++;
149 }
150 }
151 }
152
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700153 nsecs_t timeAfter = systemTime();
154 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
155 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
156}
157
Mathias Agopian3f844832013-08-07 21:24:32 -0700158ProgramCache::Key ProgramCache::computeKey(const Description& description) {
159 Key needs;
160 needs.set(Key::TEXTURE_MASK,
Alec Mouri994761f2023-08-04 21:50:55 +0000161 !description.textureEnabled ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700162 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Alec Mouri994761f2023-08-04 21:50:55 +0000163 ? Key::TEXTURE_EXT
164 : description.texture.getTextureTarget() == GL_TEXTURE_2D ? Key::TEXTURE_2D
165 : Key::TEXTURE_OFF)
Peiyong Lin46080ef2018-10-26 18:43:14 -0700166 .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800167 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700168 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800169 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700170 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700171 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800172 description.hasInputTransformMatrix() ? Key::INPUT_TRANSFORM_MATRIX_ON
173 : Key::INPUT_TRANSFORM_MATRIX_OFF)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700174 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Peiyong Lin46080ef2018-10-26 18:43:14 -0700175 description.hasOutputTransformMatrix() || description.hasColorMatrix()
176 ? Key::OUTPUT_TRANSFORM_MATRIX_ON
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700177 : Key::OUTPUT_TRANSFORM_MATRIX_OFF)
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800178 .set(Key::Key::DISPLAY_COLOR_TRANSFORM_MATRIX_MASK,
179 description.hasDisplayColorMatrix() ? Key::DISPLAY_COLOR_TRANSFORM_MATRIX_ON
180 : Key::DISPLAY_COLOR_TRANSFORM_MATRIX_OFF)
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700181 .set(Key::ROUNDED_CORNERS_MASK,
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800182 description.cornerRadius > 0 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF)
183 .set(Key::SHADOW_MASK, description.drawShadows ? Key::SHADOW_ON : Key::SHADOW_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800184
Peiyong Lin646f9342018-12-27 15:24:25 -0800185 if (needs.hasTransformMatrix() ||
186 (description.inputTransferFunction != description.outputTransferFunction)) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700187 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800188 case Description::TransferFunction::LINEAR:
189 default:
190 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
191 break;
192 case Description::TransferFunction::SRGB:
193 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
194 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800195 case Description::TransferFunction::ST2084:
196 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
197 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700198 case Description::TransferFunction::HLG:
199 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
200 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800201 }
202
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700203 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800204 case Description::TransferFunction::LINEAR:
205 default:
206 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
207 break;
208 case Description::TransferFunction::SRGB:
209 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
210 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800211 case Description::TransferFunction::ST2084:
212 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
213 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700214 case Description::TransferFunction::HLG:
215 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
216 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800217 }
218 }
219
Mathias Agopian3f844832013-08-07 21:24:32 -0700220 return needs;
221}
222
Peiyong Lin2542cb02018-04-30 17:29:53 -0700223// Generate EOTF that converts signal values to relative display light,
224// both normalized to [0, 1].
225void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
226 switch (needs.getInputTF()) {
227 case Key::INPUT_TF_SRGB:
228 fs << R"__SHADER__(
229 float EOTF_sRGB(float srgb) {
230 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
231 }
232
233 vec3 EOTF_sRGB(const vec3 srgb) {
234 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
235 }
236
237 vec3 EOTF(const vec3 srgb) {
238 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
239 }
240 )__SHADER__";
241 break;
242 case Key::INPUT_TF_ST2084:
243 fs << R"__SHADER__(
244 vec3 EOTF(const highp vec3 color) {
245 const highp float m1 = (2610.0 / 4096.0) / 4.0;
246 const highp float m2 = (2523.0 / 4096.0) * 128.0;
247 const highp float c1 = (3424.0 / 4096.0);
248 const highp float c2 = (2413.0 / 4096.0) * 32.0;
249 const highp float c3 = (2392.0 / 4096.0) * 32.0;
250
Siddharth Kapoorac1f7c92018-12-05 20:29:06 +0800251 highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700252 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
253 return pow(tmp, 1.0 / vec3(m1));
254 }
255 )__SHADER__";
256 break;
257 case Key::INPUT_TF_HLG:
258 fs << R"__SHADER__(
259 highp float EOTF_channel(const highp float channel) {
260 const highp float a = 0.17883277;
261 const highp float b = 0.28466892;
262 const highp float c = 0.55991073;
263 return channel <= 0.5 ? channel * channel / 3.0 :
264 (exp((channel - c) / a) + b) / 12.0;
265 }
266
267 vec3 EOTF(const highp vec3 color) {
268 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
269 EOTF_channel(color.b));
270 }
271 )__SHADER__";
272 break;
273 default:
274 fs << R"__SHADER__(
275 vec3 EOTF(const vec3 linear) {
276 return linear;
277 }
278 )__SHADER__";
279 break;
280 }
281}
282
Peiyong Lin53f320e2018-04-23 17:31:06 -0700283void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
284 // Convert relative light to absolute light.
285 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700286 case Key::INPUT_TF_ST2084:
287 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700288 highp vec3 ScaleLuminance(highp vec3 color) {
289 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700290 }
291 )__SHADER__";
292 break;
293 case Key::INPUT_TF_HLG:
294 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700295 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700296 // The formula is:
297 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700298 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
299 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700300 }
301 )__SHADER__";
302 break;
303 default:
304 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700305 highp vec3 ScaleLuminance(highp vec3 color) {
306 return color * displayMaxLuminance;
307 }
308 )__SHADER__";
309 break;
310 }
311
312 // Tone map absolute light to display luminance range.
313 switch (needs.getInputTF()) {
314 case Key::INPUT_TF_ST2084:
315 case Key::INPUT_TF_HLG:
316 switch (needs.getOutputTF()) {
317 case Key::OUTPUT_TF_HLG:
318 // Right now when mixed PQ and HLG contents are presented,
319 // HLG content will always be converted to PQ. However, for
320 // completeness, we simply clamp the value to [0.0, 1000.0].
321 fs << R"__SHADER__(
322 highp vec3 ToneMap(highp vec3 color) {
323 return clamp(color, 0.0, 1000.0);
324 }
325 )__SHADER__";
326 break;
327 case Key::OUTPUT_TF_ST2084:
328 fs << R"__SHADER__(
329 highp vec3 ToneMap(highp vec3 color) {
330 return color;
331 }
332 )__SHADER__";
333 break;
334 default:
335 fs << R"__SHADER__(
336 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800337 float maxMasteringLumi = maxMasteringLuminance;
338 float maxContentLumi = maxContentLuminance;
339 float maxInLumi = min(maxMasteringLumi, maxContentLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700340 float maxOutLumi = displayMaxLuminance;
341
342 float nits = color.y;
343
344 // clamp to max input luminance
345 nits = clamp(nits, 0.0, maxInLumi);
346
347 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
348 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700349 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700350 } else {
351 // three control points
352 const float x0 = 10.0;
353 const float y0 = 17.0;
354 float x1 = maxOutLumi * 0.75;
355 float y1 = x1;
356 float x2 = x1 + (maxInLumi - x1) / 2.0;
357 float y2 = y1 + (maxOutLumi - y1) * 0.75;
358
359 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700360 float h12 = x2 - x1;
361 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700362 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700363 float m1 = (y2 - y1) / h12;
364 float m3 = (maxOutLumi - y2) / h23;
365 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700366
367 if (nits < x0) {
368 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700369 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700370 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700371 } else if (nits < x1) {
372 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700373 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700374 nits = y0 + (nits - x0) * slope;
375 } else if (nits < x2) {
376 // scale [x1, x2] to [y1, y2] using Hermite interp
377 float t = (nits - x1) / h12;
378 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
379 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
380 } else {
381 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
382 float t = (nits - x2) / h23;
383 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
384 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
385 }
386 }
387
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700388 // color.y is greater than x0 and is thus non-zero
389 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700390 }
391 )__SHADER__";
392 break;
393 }
394 break;
395 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700396 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700397 fs << R"__SHADER__(
398 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700399 const float maxOutLumi = 3000.0;
400
401 const float x0 = 5.0;
402 const float y0 = 2.5;
403 float x1 = displayMaxLuminance * 0.7;
404 float y1 = maxOutLumi * 0.15;
405 float x2 = displayMaxLuminance * 0.9;
406 float y2 = maxOutLumi * 0.45;
407 float x3 = displayMaxLuminance;
408 float y3 = maxOutLumi;
409
410 float c1 = y1 / 3.0;
411 float c2 = y2 / 2.0;
412 float c3 = y3 / 1.5;
413
414 float nits = color.y;
415
416 float scale;
417 if (nits <= x0) {
418 // scale [0.0, x0] to [0.0, y0] linearly
419 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700420 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700421 } else if (nits <= x1) {
422 // scale [x0, x1] to [y0, y1] using a curve
423 float t = (nits - x0) / (x1 - x0);
424 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
425 } else if (nits <= x2) {
426 // scale [x1, x2] to [y1, y2] using a curve
427 float t = (nits - x1) / (x2 - x1);
428 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
429 } else {
430 // scale [x2, x3] to [y2, y3] using a curve
431 float t = (nits - x2) / (x3 - x2);
432 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
433 }
434
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700435 // color.y is greater than x0 and is thus non-zero
436 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700437 }
438 )__SHADER__";
439 break;
440 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700441
442 // convert absolute light to relative light.
443 switch (needs.getOutputTF()) {
444 case Key::OUTPUT_TF_ST2084:
445 fs << R"__SHADER__(
446 highp vec3 NormalizeLuminance(highp vec3 color) {
447 return color / 10000.0;
448 }
449 )__SHADER__";
450 break;
451 case Key::OUTPUT_TF_HLG:
452 fs << R"__SHADER__(
453 highp vec3 NormalizeLuminance(highp vec3 color) {
454 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
455 }
456 )__SHADER__";
457 break;
458 default:
459 fs << R"__SHADER__(
460 highp vec3 NormalizeLuminance(highp vec3 color) {
461 return color / displayMaxLuminance;
462 }
463 )__SHADER__";
464 break;
465 }
466}
467
468// Generate OOTF that modifies the relative scence light to relative display light.
469void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
470 if (!needs.needsToneMapping()) {
471 fs << R"__SHADER__(
472 highp vec3 OOTF(const highp vec3 color) {
473 return color;
474 }
475 )__SHADER__";
476 } else {
477 generateToneMappingProcess(fs, needs);
478 fs << R"__SHADER__(
479 highp vec3 OOTF(const highp vec3 color) {
480 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
481 }
482 )__SHADER__";
483 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700484}
485
486// Generate OETF that converts relative display light to signal values,
487// both normalized to [0, 1]
488void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
489 switch (needs.getOutputTF()) {
490 case Key::OUTPUT_TF_SRGB:
491 fs << R"__SHADER__(
492 float OETF_sRGB(const float linear) {
493 return linear <= 0.0031308 ?
494 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
495 }
496
497 vec3 OETF_sRGB(const vec3 linear) {
498 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
499 }
500
501 vec3 OETF(const vec3 linear) {
502 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
503 }
504 )__SHADER__";
505 break;
506 case Key::OUTPUT_TF_ST2084:
507 fs << R"__SHADER__(
508 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700509 const highp float m1 = (2610.0 / 4096.0) / 4.0;
510 const highp float m2 = (2523.0 / 4096.0) * 128.0;
511 const highp float c1 = (3424.0 / 4096.0);
512 const highp float c2 = (2413.0 / 4096.0) * 32.0;
513 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700514
Peiyong Lin834be492018-05-18 15:12:55 -0700515 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700516 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
517 return pow(tmp, vec3(m2));
518 }
519 )__SHADER__";
520 break;
521 case Key::OUTPUT_TF_HLG:
522 fs << R"__SHADER__(
523 highp float OETF_channel(const highp float channel) {
524 const highp float a = 0.17883277;
525 const highp float b = 0.28466892;
526 const highp float c = 0.55991073;
527 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
528 a * log(12.0 * channel - b) + c;
529 }
530
531 vec3 OETF(const highp vec3 color) {
532 return vec3(OETF_channel(color.r), OETF_channel(color.g),
533 OETF_channel(color.b));
534 }
535 )__SHADER__";
536 break;
537 default:
538 fs << R"__SHADER__(
539 vec3 OETF(const vec3 linear) {
540 return linear;
541 }
542 )__SHADER__";
543 break;
544 }
545}
546
Mathias Agopian3f844832013-08-07 21:24:32 -0700547String8 ProgramCache::generateVertexShader(const Key& needs) {
548 Formatter vs;
Vishnu Nairf19544f2020-02-03 11:23:26 -0800549 if (needs.hasTextureCoords()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800550 vs << "attribute vec4 texCoords;"
551 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700552 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700553 if (needs.hasRoundedCorners()) {
554 vs << "attribute lowp vec4 cropCoords;";
555 vs << "varying lowp vec2 outCropCoords;";
556 }
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800557 if (needs.drawShadows()) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800558 vs << "attribute lowp vec4 shadowColor;";
559 vs << "varying lowp vec4 outShadowColor;";
560 vs << "attribute lowp vec4 shadowParams;";
561 vs << "varying lowp vec3 outShadowParams;";
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800562 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700563 vs << "attribute vec4 position;"
564 << "uniform mat4 projection;"
565 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800566 << "void main(void) {" << indent << "gl_Position = projection * position;";
Vishnu Nairf19544f2020-02-03 11:23:26 -0800567 if (needs.hasTextureCoords()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700568 vs << "outTexCoords = (texture * texCoords).st;";
569 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700570 if (needs.hasRoundedCorners()) {
571 vs << "outCropCoords = cropCoords.st;";
572 }
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800573 if (needs.drawShadows()) {
574 vs << "outShadowColor = shadowColor;";
575 vs << "outShadowParams = shadowParams.xyz;";
576 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700577 vs << dedent << "}";
578 return vs.getString();
579}
580
581String8 ProgramCache::generateFragmentShader(const Key& needs) {
582 Formatter fs;
583 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
584 fs << "#extension GL_OES_EGL_image_external : require";
585 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700586
587 // default precision is required-ish in fragment shaders
588 fs << "precision mediump float;";
589
Mathias Agopian3f844832013-08-07 21:24:32 -0700590 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800591 fs << "uniform samplerExternalOES sampler;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700592 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800593 fs << "uniform sampler2D sampler;";
594 }
595
596 if (needs.hasTextureCoords()) {
Chia-I Wu82bafb32022-10-19 14:41:59 -0700597 fs << "varying highp vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700598 }
599
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700600 if (needs.hasRoundedCorners()) {
601 // Rounded corners implementation using a signed distance function.
602 fs << R"__SHADER__(
603 uniform float cornerRadius;
604 uniform vec2 cropCenter;
605 varying vec2 outCropCoords;
606
607 /**
608 * This function takes the current crop coordinates and calculates an alpha value based
609 * on the corner radius and distance from the crop center.
610 */
611 float applyCornerRadius(vec2 cropCoords)
612 {
613 vec2 position = cropCoords - cropCenter;
Alec Mouri0d0e16e2019-07-25 15:22:29 -0700614 // Scale down the dist vector here, as otherwise large corner
615 // radii can cause floating point issues when computing the norm
616 vec2 dist = (abs(position) - cropCenter + vec2(cornerRadius)) / 16.0;
617 // Once we've found the norm, then scale back up.
618 float plane = length(max(dist, vec2(0.0))) * 16.0;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700619 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
620 }
621 )__SHADER__";
622 }
623
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800624 if (needs.drawShadows()) {
625 fs << R"__SHADER__(
Vishnu Nairf19544f2020-02-03 11:23:26 -0800626 varying lowp vec4 outShadowColor;
627 varying lowp vec3 outShadowParams;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800628
629 /**
630 * Returns the shadow color.
631 */
632 vec4 getShadowColor()
633 {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800634 lowp float d = length(outShadowParams.xy);
635 vec2 uv = vec2(outShadowParams.z * (1.0 - d), 0.5);
636 lowp float factor = texture2D(sampler, uv).a;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800637 return outShadowColor * factor;
638 }
639 )__SHADER__";
640 }
641
chaviw13fdc492017-06-27 12:40:18 -0700642 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700643 fs << "uniform vec4 color;";
644 }
chaviw13fdc492017-06-27 12:40:18 -0700645
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800646 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF()) ||
647 needs.hasDisplayColorMatrix()) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700648 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700649 fs << "uniform float displayMaxLuminance;";
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800650 fs << "uniform float maxMasteringLuminance;";
651 fs << "uniform float maxContentLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700652 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700653
Peiyong Lina296b0c2018-04-30 16:55:29 -0700654 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700655 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700656 fs << R"__SHADER__(
657 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700658 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700659 }
660 )__SHADER__";
661 } else {
662 fs << R"__SHADER__(
663 highp vec3 InputTransform(const highp vec3 color) {
664 return color;
665 }
666 )__SHADER__";
667 }
668
669 // the transformation from a wider colorspace to a narrower one can
670 // result in >1.0 or <0.0 pixel values
671 if (needs.hasOutputTransformMatrix()) {
672 fs << "uniform mat4 outputTransformMatrix;";
673 fs << R"__SHADER__(
674 highp vec3 OutputTransform(const highp vec3 color) {
675 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
676 }
677 )__SHADER__";
678 } else {
679 fs << R"__SHADER__(
680 highp vec3 OutputTransform(const highp vec3 color) {
681 return clamp(color, 0.0, 1.0);
682 }
683 )__SHADER__";
684 }
685
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800686 if (needs.hasDisplayColorMatrix()) {
687 fs << "uniform mat4 displayColorMatrix;";
688 fs << R"__SHADER__(
689 highp vec3 DisplayColorMatrix(const highp vec3 color) {
690 return clamp(vec3(displayColorMatrix * vec4(color, 1.0)), 0.0, 1.0);
691 }
692 )__SHADER__";
693 } else {
694 fs << R"__SHADER__(
695 highp vec3 DisplayColorMatrix(const highp vec3 color) {
696 return color;
697 }
698 )__SHADER__";
699 }
700
Peiyong Lin2542cb02018-04-30 17:29:53 -0700701 generateEOTF(fs, needs);
702 generateOOTF(fs, needs);
703 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700704 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800705
Mathias Agopian3f844832013-08-07 21:24:32 -0700706 fs << "void main(void) {" << indent;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800707 if (needs.drawShadows()) {
708 fs << "gl_FragColor = getShadowColor();";
Mathias Agopian3f844832013-08-07 21:24:32 -0700709 } else {
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800710 if (needs.isTexturing()) {
711 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Mathias Agopian3f844832013-08-07 21:24:32 -0700712 } else {
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800713 fs << "gl_FragColor.rgb = color.rgb;";
714 fs << "gl_FragColor.a = 1.0;";
715 }
716 if (needs.isOpaque()) {
717 fs << "gl_FragColor.a = 1.0;";
718 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700719 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700720
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800721 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF()) ||
722 needs.hasDisplayColorMatrix()) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700723 if (!needs.isOpaque() && needs.isPremultiplied()) {
724 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700725 // avoid divide by 0 by adding 0.5/256 to the alpha channel
726 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700727 }
Peiyong Lin46080ef2018-10-26 18:43:14 -0700728 fs << "gl_FragColor.rgb = "
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800729 "DisplayColorMatrix(OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))"
730 ")));";
731
Mathias Agopianff2ed702013-09-01 21:36:12 -0700732 if (!needs.isOpaque() && needs.isPremultiplied()) {
733 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700734 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700735 }
736 }
737
KaiChieh Chuangda2845c2020-12-14 16:49:38 +0800738 /*
739 * Whether applying layer alpha before or after color transform doesn't matter,
740 * as long as we can undo premultiplication. But we cannot un-premultiply
741 * for color transform if the layer alpha = 0, e.g. 0 / (0 + 0.0019) = 0.
742 */
743 if (!needs.drawShadows()) {
744 if (needs.hasAlpha()) {
745 // modulate the current alpha value with alpha set
746 if (needs.isPremultiplied()) {
747 // ... and the color too if we're premultiplied
748 fs << "gl_FragColor *= color.a;";
749 } else {
750 fs << "gl_FragColor.a *= color.a;";
751 }
752 }
753 }
754
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700755 if (needs.hasRoundedCorners()) {
756 if (needs.isPremultiplied()) {
757 fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
758 } else {
759 fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
760 }
761 }
762
Mathias Agopian3f844832013-08-07 21:24:32 -0700763 fs << dedent << "}";
764 return fs.getString();
765}
766
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700767std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700768 ATRACE_CALL();
769
Mathias Agopian3f844832013-08-07 21:24:32 -0700770 // vertex shader
771 String8 vs = generateVertexShader(needs);
772
773 // fragment shader
774 String8 fs = generateFragmentShader(needs);
775
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700776 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700777}
778
Peiyong Linfb530cf2018-12-15 05:07:38 +0000779void ProgramCache::useProgram(EGLContext context, const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700780 // generate the key for the shader based on the description
781 Key needs(computeKey(description));
782
Chia-I Wub027f802017-11-29 14:00:52 -0800783 // look-up the program in the cache
Peiyong Linfb530cf2018-12-15 05:07:38 +0000784 auto& cache = mCaches[context];
785 auto it = cache.find(needs);
786 if (it == cache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700787 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700788 nsecs_t time = systemTime();
Peiyong Linfb530cf2018-12-15 05:07:38 +0000789 it = cache.emplace(needs, generateProgram(needs)).first;
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700790 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700791
Peiyong Linfb530cf2018-12-15 05:07:38 +0000792 ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)",
793 context, needs.mKey, uint32_t(ns2ms(time)), cache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700794 }
795
796 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700797 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700798 if (program->isValid()) {
799 program->use();
800 program->setUniforms(description);
801 }
802}
803
Peiyong Lin46080ef2018-10-26 18:43:14 -0700804} // namespace gl
805} // namespace renderengine
806} // namespace android