blob: e2757e181b29da5f1ea75bf8e43f4631638a5a69 [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
101 // Cache Y410 input on or off
102 shaderKey.set(Key::Y410_BT2020_MASK, (i & 2) ?
103 Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
104 if (cache.count(shaderKey) == 0) {
105 cache.emplace(shaderKey, generateProgram(shaderKey));
106 shaderCount++;
107 }
108 }
109 return;
110 }
111
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700112 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK
113 | Key::ROUNDED_CORNERS_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700114 // Prime the cache for all combinations of the above masks,
115 // leaving off the experimental color matrix mask options.
116
117 nsecs_t timeBefore = systemTime();
118 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
119 Key shaderKey;
120 shaderKey.set(keyMask, keyVal);
121 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -0800122 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700123 continue;
124 }
Peiyong Linfb530cf2018-12-15 05:07:38 +0000125 if (cache.count(shaderKey) == 0) {
126 cache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700127 shaderCount++;
128 }
129 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700130
131 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700132 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700133 Key shaderKey;
Peiyong Linc1bb20c2019-03-25 11:06:12 -0700134 shaderKey.set(Key::BLEND_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::INPUT_TF_MASK |
135 Key::OUTPUT_TF_MASK,
136 Key::BLEND_PREMULT | Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::INPUT_TF_SRGB |
137 Key::OUTPUT_TF_SRGB);
138 for (int i = 0; i < 16; i++) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700139 shaderKey.set(Key::OPACITY_MASK,
140 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
141 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Peiyong Linc1bb20c2019-03-25 11:06:12 -0700142
143 // Cache rounded corners
144 shaderKey.set(Key::ROUNDED_CORNERS_MASK,
145 (i & 4) ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
146
147 // Cache texture off option for window transition
148 shaderKey.set(Key::TEXTURE_MASK, (i & 8) ? Key::TEXTURE_EXT : Key::TEXTURE_OFF);
Peiyong Linfb530cf2018-12-15 05:07:38 +0000149 if (cache.count(shaderKey) == 0) {
150 cache.emplace(shaderKey, generateProgram(shaderKey));
Chia-I Wu93e14df2018-06-04 10:10:17 -0700151 shaderCount++;
152 }
153 }
154 }
155
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700156 nsecs_t timeAfter = systemTime();
157 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
158 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
159}
160
Mathias Agopian3f844832013-08-07 21:24:32 -0700161ProgramCache::Key ProgramCache::computeKey(const Description& description) {
162 Key needs;
163 needs.set(Key::TEXTURE_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700164 !description.textureEnabled
Chia-I Wub027f802017-11-29 14:00:52 -0800165 ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700166 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Chia-I Wub027f802017-11-29 14:00:52 -0800167 ? Key::TEXTURE_EXT
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700168 : description.texture.getTextureTarget() == GL_TEXTURE_2D
Chia-I Wub027f802017-11-29 14:00:52 -0800169 ? Key::TEXTURE_2D
170 : Key::TEXTURE_OFF)
Peiyong Lin46080ef2018-10-26 18:43:14 -0700171 .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800172 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700173 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800174 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700175 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700176 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700177 description.hasInputTransformMatrix()
178 ? Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700179 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Peiyong Lin46080ef2018-10-26 18:43:14 -0700180 description.hasOutputTransformMatrix() || description.hasColorMatrix()
181 ? Key::OUTPUT_TRANSFORM_MATRIX_ON
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700182 : Key::OUTPUT_TRANSFORM_MATRIX_OFF)
183 .set(Key::ROUNDED_CORNERS_MASK,
184 description.cornerRadius > 0
185 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800186
Chia-I Wu131d3762018-01-11 14:35:27 -0800187 needs.set(Key::Y410_BT2020_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700188 description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800189
Peiyong Lin646f9342018-12-27 15:24:25 -0800190 if (needs.hasTransformMatrix() ||
191 (description.inputTransferFunction != description.outputTransferFunction)) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700192 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800193 case Description::TransferFunction::LINEAR:
194 default:
195 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
196 break;
197 case Description::TransferFunction::SRGB:
198 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
199 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800200 case Description::TransferFunction::ST2084:
201 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
202 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700203 case Description::TransferFunction::HLG:
204 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
205 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800206 }
207
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700208 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800209 case Description::TransferFunction::LINEAR:
210 default:
211 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
212 break;
213 case Description::TransferFunction::SRGB:
214 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
215 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800216 case Description::TransferFunction::ST2084:
217 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
218 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700219 case Description::TransferFunction::HLG:
220 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
221 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800222 }
223 }
224
Mathias Agopian3f844832013-08-07 21:24:32 -0700225 return needs;
226}
227
Peiyong Lin2542cb02018-04-30 17:29:53 -0700228// Generate EOTF that converts signal values to relative display light,
229// both normalized to [0, 1].
230void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
231 switch (needs.getInputTF()) {
232 case Key::INPUT_TF_SRGB:
233 fs << R"__SHADER__(
234 float EOTF_sRGB(float srgb) {
235 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
236 }
237
238 vec3 EOTF_sRGB(const vec3 srgb) {
239 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
240 }
241
242 vec3 EOTF(const vec3 srgb) {
243 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
244 }
245 )__SHADER__";
246 break;
247 case Key::INPUT_TF_ST2084:
248 fs << R"__SHADER__(
249 vec3 EOTF(const highp vec3 color) {
250 const highp float m1 = (2610.0 / 4096.0) / 4.0;
251 const highp float m2 = (2523.0 / 4096.0) * 128.0;
252 const highp float c1 = (3424.0 / 4096.0);
253 const highp float c2 = (2413.0 / 4096.0) * 32.0;
254 const highp float c3 = (2392.0 / 4096.0) * 32.0;
255
Siddharth Kapoorac1f7c92018-12-05 20:29:06 +0800256 highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700257 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
258 return pow(tmp, 1.0 / vec3(m1));
259 }
260 )__SHADER__";
261 break;
262 case Key::INPUT_TF_HLG:
263 fs << R"__SHADER__(
264 highp float EOTF_channel(const highp float channel) {
265 const highp float a = 0.17883277;
266 const highp float b = 0.28466892;
267 const highp float c = 0.55991073;
268 return channel <= 0.5 ? channel * channel / 3.0 :
269 (exp((channel - c) / a) + b) / 12.0;
270 }
271
272 vec3 EOTF(const highp vec3 color) {
273 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
274 EOTF_channel(color.b));
275 }
276 )__SHADER__";
277 break;
278 default:
279 fs << R"__SHADER__(
280 vec3 EOTF(const vec3 linear) {
281 return linear;
282 }
283 )__SHADER__";
284 break;
285 }
286}
287
Peiyong Lin53f320e2018-04-23 17:31:06 -0700288void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
289 // Convert relative light to absolute light.
290 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700291 case Key::INPUT_TF_ST2084:
292 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700293 highp vec3 ScaleLuminance(highp vec3 color) {
294 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700295 }
296 )__SHADER__";
297 break;
298 case Key::INPUT_TF_HLG:
299 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700300 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700301 // The formula is:
302 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700303 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
304 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700305 }
306 )__SHADER__";
307 break;
308 default:
309 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700310 highp vec3 ScaleLuminance(highp vec3 color) {
311 return color * displayMaxLuminance;
312 }
313 )__SHADER__";
314 break;
315 }
316
317 // Tone map absolute light to display luminance range.
318 switch (needs.getInputTF()) {
319 case Key::INPUT_TF_ST2084:
320 case Key::INPUT_TF_HLG:
321 switch (needs.getOutputTF()) {
322 case Key::OUTPUT_TF_HLG:
323 // Right now when mixed PQ and HLG contents are presented,
324 // HLG content will always be converted to PQ. However, for
325 // completeness, we simply clamp the value to [0.0, 1000.0].
326 fs << R"__SHADER__(
327 highp vec3 ToneMap(highp vec3 color) {
328 return clamp(color, 0.0, 1000.0);
329 }
330 )__SHADER__";
331 break;
332 case Key::OUTPUT_TF_ST2084:
333 fs << R"__SHADER__(
334 highp vec3 ToneMap(highp vec3 color) {
335 return color;
336 }
337 )__SHADER__";
338 break;
339 default:
340 fs << R"__SHADER__(
341 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800342 float maxMasteringLumi = maxMasteringLuminance;
343 float maxContentLumi = maxContentLuminance;
344 float maxInLumi = min(maxMasteringLumi, maxContentLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700345 float maxOutLumi = displayMaxLuminance;
346
347 float nits = color.y;
348
349 // clamp to max input luminance
350 nits = clamp(nits, 0.0, maxInLumi);
351
352 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
353 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700354 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700355 } else {
356 // three control points
357 const float x0 = 10.0;
358 const float y0 = 17.0;
359 float x1 = maxOutLumi * 0.75;
360 float y1 = x1;
361 float x2 = x1 + (maxInLumi - x1) / 2.0;
362 float y2 = y1 + (maxOutLumi - y1) * 0.75;
363
364 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700365 float h12 = x2 - x1;
366 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700367 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700368 float m1 = (y2 - y1) / h12;
369 float m3 = (maxOutLumi - y2) / h23;
370 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700371
372 if (nits < x0) {
373 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700374 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700375 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700376 } else if (nits < x1) {
377 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700378 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700379 nits = y0 + (nits - x0) * slope;
380 } else if (nits < x2) {
381 // scale [x1, x2] to [y1, y2] using Hermite interp
382 float t = (nits - x1) / h12;
383 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
384 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
385 } else {
386 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
387 float t = (nits - x2) / h23;
388 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
389 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
390 }
391 }
392
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700393 // color.y is greater than x0 and is thus non-zero
394 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700395 }
396 )__SHADER__";
397 break;
398 }
399 break;
400 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700401 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700402 fs << R"__SHADER__(
403 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700404 const float maxOutLumi = 3000.0;
405
406 const float x0 = 5.0;
407 const float y0 = 2.5;
408 float x1 = displayMaxLuminance * 0.7;
409 float y1 = maxOutLumi * 0.15;
410 float x2 = displayMaxLuminance * 0.9;
411 float y2 = maxOutLumi * 0.45;
412 float x3 = displayMaxLuminance;
413 float y3 = maxOutLumi;
414
415 float c1 = y1 / 3.0;
416 float c2 = y2 / 2.0;
417 float c3 = y3 / 1.5;
418
419 float nits = color.y;
420
421 float scale;
422 if (nits <= x0) {
423 // scale [0.0, x0] to [0.0, y0] linearly
424 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700425 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700426 } else if (nits <= x1) {
427 // scale [x0, x1] to [y0, y1] using a curve
428 float t = (nits - x0) / (x1 - x0);
429 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
430 } else if (nits <= x2) {
431 // scale [x1, x2] to [y1, y2] using a curve
432 float t = (nits - x1) / (x2 - x1);
433 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
434 } else {
435 // scale [x2, x3] to [y2, y3] using a curve
436 float t = (nits - x2) / (x3 - x2);
437 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
438 }
439
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700440 // color.y is greater than x0 and is thus non-zero
441 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700442 }
443 )__SHADER__";
444 break;
445 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700446
447 // convert absolute light to relative light.
448 switch (needs.getOutputTF()) {
449 case Key::OUTPUT_TF_ST2084:
450 fs << R"__SHADER__(
451 highp vec3 NormalizeLuminance(highp vec3 color) {
452 return color / 10000.0;
453 }
454 )__SHADER__";
455 break;
456 case Key::OUTPUT_TF_HLG:
457 fs << R"__SHADER__(
458 highp vec3 NormalizeLuminance(highp vec3 color) {
459 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
460 }
461 )__SHADER__";
462 break;
463 default:
464 fs << R"__SHADER__(
465 highp vec3 NormalizeLuminance(highp vec3 color) {
466 return color / displayMaxLuminance;
467 }
468 )__SHADER__";
469 break;
470 }
471}
472
473// Generate OOTF that modifies the relative scence light to relative display light.
474void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
475 if (!needs.needsToneMapping()) {
476 fs << R"__SHADER__(
477 highp vec3 OOTF(const highp vec3 color) {
478 return color;
479 }
480 )__SHADER__";
481 } else {
482 generateToneMappingProcess(fs, needs);
483 fs << R"__SHADER__(
484 highp vec3 OOTF(const highp vec3 color) {
485 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
486 }
487 )__SHADER__";
488 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700489}
490
491// Generate OETF that converts relative display light to signal values,
492// both normalized to [0, 1]
493void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
494 switch (needs.getOutputTF()) {
495 case Key::OUTPUT_TF_SRGB:
496 fs << R"__SHADER__(
497 float OETF_sRGB(const float linear) {
498 return linear <= 0.0031308 ?
499 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
500 }
501
502 vec3 OETF_sRGB(const vec3 linear) {
503 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
504 }
505
506 vec3 OETF(const vec3 linear) {
507 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
508 }
509 )__SHADER__";
510 break;
511 case Key::OUTPUT_TF_ST2084:
512 fs << R"__SHADER__(
513 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700514 const highp float m1 = (2610.0 / 4096.0) / 4.0;
515 const highp float m2 = (2523.0 / 4096.0) * 128.0;
516 const highp float c1 = (3424.0 / 4096.0);
517 const highp float c2 = (2413.0 / 4096.0) * 32.0;
518 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700519
Peiyong Lin834be492018-05-18 15:12:55 -0700520 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700521 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
522 return pow(tmp, vec3(m2));
523 }
524 )__SHADER__";
525 break;
526 case Key::OUTPUT_TF_HLG:
527 fs << R"__SHADER__(
528 highp float OETF_channel(const highp float channel) {
529 const highp float a = 0.17883277;
530 const highp float b = 0.28466892;
531 const highp float c = 0.55991073;
532 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
533 a * log(12.0 * channel - b) + c;
534 }
535
536 vec3 OETF(const highp vec3 color) {
537 return vec3(OETF_channel(color.r), OETF_channel(color.g),
538 OETF_channel(color.b));
539 }
540 )__SHADER__";
541 break;
542 default:
543 fs << R"__SHADER__(
544 vec3 OETF(const vec3 linear) {
545 return linear;
546 }
547 )__SHADER__";
548 break;
549 }
550}
551
Mathias Agopian3f844832013-08-07 21:24:32 -0700552String8 ProgramCache::generateVertexShader(const Key& needs) {
553 Formatter vs;
554 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800555 vs << "attribute vec4 texCoords;"
556 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700557 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700558 if (needs.hasRoundedCorners()) {
559 vs << "attribute lowp vec4 cropCoords;";
560 vs << "varying lowp vec2 outCropCoords;";
561 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700562 vs << "attribute vec4 position;"
563 << "uniform mat4 projection;"
564 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800565 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700566 if (needs.isTexturing()) {
567 vs << "outTexCoords = (texture * texCoords).st;";
568 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700569 if (needs.hasRoundedCorners()) {
570 vs << "outCropCoords = cropCoords.st;";
571 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700572 vs << dedent << "}";
573 return vs.getString();
574}
575
576String8 ProgramCache::generateFragmentShader(const Key& needs) {
577 Formatter fs;
578 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
579 fs << "#extension GL_OES_EGL_image_external : require";
580 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700581
582 // default precision is required-ish in fragment shaders
583 fs << "precision mediump float;";
584
Mathias Agopian3f844832013-08-07 21:24:32 -0700585 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
586 fs << "uniform samplerExternalOES sampler;"
587 << "varying vec2 outTexCoords;";
588 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
589 fs << "uniform sampler2D sampler;"
590 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700591 }
592
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700593 if (needs.hasRoundedCorners()) {
594 // Rounded corners implementation using a signed distance function.
595 fs << R"__SHADER__(
596 uniform float cornerRadius;
597 uniform vec2 cropCenter;
598 varying vec2 outCropCoords;
599
600 /**
601 * This function takes the current crop coordinates and calculates an alpha value based
602 * on the corner radius and distance from the crop center.
603 */
604 float applyCornerRadius(vec2 cropCoords)
605 {
606 vec2 position = cropCoords - cropCenter;
Alec Mouri0d0e16e2019-07-25 15:22:29 -0700607 // Scale down the dist vector here, as otherwise large corner
608 // radii can cause floating point issues when computing the norm
609 vec2 dist = (abs(position) - cropCenter + vec2(cornerRadius)) / 16.0;
610 // Once we've found the norm, then scale back up.
611 float plane = length(max(dist, vec2(0.0))) * 16.0;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700612 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
613 }
614 )__SHADER__";
615 }
616
chaviw13fdc492017-06-27 12:40:18 -0700617 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700618 fs << "uniform vec4 color;";
619 }
chaviw13fdc492017-06-27 12:40:18 -0700620
Chia-I Wu131d3762018-01-11 14:35:27 -0800621 if (needs.isY410BT2020()) {
622 fs << R"__SHADER__(
623 vec3 convertY410BT2020(const vec3 color) {
624 const vec3 offset = vec3(0.0625, 0.5, 0.5);
625 const mat3 transform = mat3(
626 vec3(1.1678, 1.1678, 1.1678),
627 vec3( 0.0, -0.1878, 2.1481),
628 vec3(1.6836, -0.6523, 0.0));
629 // Y is in G, U is in R, and V is in B
630 return clamp(transform * (color.grb - offset), 0.0, 1.0);
631 }
632 )__SHADER__";
633 }
634
Peiyong Lina296b0c2018-04-30 16:55:29 -0700635 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700636 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700637 fs << "uniform float displayMaxLuminance;";
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800638 fs << "uniform float maxMasteringLuminance;";
639 fs << "uniform float maxContentLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700640 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700641
Peiyong Lina296b0c2018-04-30 16:55:29 -0700642 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700643 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700644 fs << R"__SHADER__(
645 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700646 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700647 }
648 )__SHADER__";
649 } else {
650 fs << R"__SHADER__(
651 highp vec3 InputTransform(const highp vec3 color) {
652 return color;
653 }
654 )__SHADER__";
655 }
656
657 // the transformation from a wider colorspace to a narrower one can
658 // result in >1.0 or <0.0 pixel values
659 if (needs.hasOutputTransformMatrix()) {
660 fs << "uniform mat4 outputTransformMatrix;";
661 fs << R"__SHADER__(
662 highp vec3 OutputTransform(const highp vec3 color) {
663 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
664 }
665 )__SHADER__";
666 } else {
667 fs << R"__SHADER__(
668 highp vec3 OutputTransform(const highp vec3 color) {
669 return clamp(color, 0.0, 1.0);
670 }
671 )__SHADER__";
672 }
673
Peiyong Lin2542cb02018-04-30 17:29:53 -0700674 generateEOTF(fs, needs);
675 generateOOTF(fs, needs);
676 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700677 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800678
Mathias Agopian3f844832013-08-07 21:24:32 -0700679 fs << "void main(void) {" << indent;
680 if (needs.isTexturing()) {
681 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800682 if (needs.isY410BT2020()) {
683 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
684 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700685 } else {
chaviw13fdc492017-06-27 12:40:18 -0700686 fs << "gl_FragColor.rgb = color.rgb;";
687 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700688 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700689 if (needs.isOpaque()) {
690 fs << "gl_FragColor.a = 1.0;";
691 }
chaviw13fdc492017-06-27 12:40:18 -0700692 if (needs.hasAlpha()) {
693 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700694 if (needs.isPremultiplied()) {
695 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700696 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700697 } else {
chaviw13fdc492017-06-27 12:40:18 -0700698 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700699 }
700 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700701
Peiyong Lina296b0c2018-04-30 16:55:29 -0700702 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700703 if (!needs.isOpaque() && needs.isPremultiplied()) {
704 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700705 // avoid divide by 0 by adding 0.5/256 to the alpha channel
706 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700707 }
Peiyong Lin46080ef2018-10-26 18:43:14 -0700708 fs << "gl_FragColor.rgb = "
709 "OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700710 if (!needs.isOpaque() && needs.isPremultiplied()) {
711 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700712 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700713 }
714 }
715
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700716 if (needs.hasRoundedCorners()) {
717 if (needs.isPremultiplied()) {
718 fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
719 } else {
720 fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
721 }
722 }
723
Mathias Agopian3f844832013-08-07 21:24:32 -0700724 fs << dedent << "}";
725 return fs.getString();
726}
727
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700728std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700729 ATRACE_CALL();
730
Mathias Agopian3f844832013-08-07 21:24:32 -0700731 // vertex shader
732 String8 vs = generateVertexShader(needs);
733
734 // fragment shader
735 String8 fs = generateFragmentShader(needs);
736
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700737 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700738}
739
Peiyong Linfb530cf2018-12-15 05:07:38 +0000740void ProgramCache::useProgram(EGLContext context, const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700741 // generate the key for the shader based on the description
742 Key needs(computeKey(description));
743
Chia-I Wub027f802017-11-29 14:00:52 -0800744 // look-up the program in the cache
Peiyong Linfb530cf2018-12-15 05:07:38 +0000745 auto& cache = mCaches[context];
746 auto it = cache.find(needs);
747 if (it == cache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700748 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700749 nsecs_t time = systemTime();
Peiyong Linfb530cf2018-12-15 05:07:38 +0000750 it = cache.emplace(needs, generateProgram(needs)).first;
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700751 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700752
Peiyong Linfb530cf2018-12-15 05:07:38 +0000753 ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)",
754 context, needs.mKey, uint32_t(ns2ms(time)), cache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700755 }
756
757 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700758 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700759 if (program->isValid()) {
760 program->use();
761 program->setUniforms(description);
762 }
763}
764
Peiyong Lin46080ef2018-10-26 18:43:14 -0700765} // namespace gl
766} // namespace renderengine
767} // namespace android