blob: dc8ce54736ebfad9c32f78a68fb6272743fe7c36 [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,
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800177 description.hasInputTransformMatrix() ? Key::INPUT_TRANSFORM_MATRIX_ON
178 : 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)
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800183 .set(Key::Key::DISPLAY_COLOR_TRANSFORM_MATRIX_MASK,
184 description.hasDisplayColorMatrix()
185 ? Key::DISPLAY_COLOR_TRANSFORM_MATRIX_ON
186 : Key::DISPLAY_COLOR_TRANSFORM_MATRIX_OFF)
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700187 .set(Key::ROUNDED_CORNERS_MASK,
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800188 description.cornerRadius > 0 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF)
189 .set(Key::SHADOW_MASK, description.drawShadows ? Key::SHADOW_ON : Key::SHADOW_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800190 needs.set(Key::Y410_BT2020_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700191 description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800192
Peiyong Lin646f9342018-12-27 15:24:25 -0800193 if (needs.hasTransformMatrix() ||
194 (description.inputTransferFunction != description.outputTransferFunction)) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700195 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800196 case Description::TransferFunction::LINEAR:
197 default:
198 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
199 break;
200 case Description::TransferFunction::SRGB:
201 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
202 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800203 case Description::TransferFunction::ST2084:
204 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
205 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700206 case Description::TransferFunction::HLG:
207 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
208 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800209 }
210
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700211 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800212 case Description::TransferFunction::LINEAR:
213 default:
214 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
215 break;
216 case Description::TransferFunction::SRGB:
217 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
218 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800219 case Description::TransferFunction::ST2084:
220 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
221 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700222 case Description::TransferFunction::HLG:
223 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
224 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800225 }
226 }
227
Mathias Agopian3f844832013-08-07 21:24:32 -0700228 return needs;
229}
230
Peiyong Lin2542cb02018-04-30 17:29:53 -0700231// Generate EOTF that converts signal values to relative display light,
232// both normalized to [0, 1].
233void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
234 switch (needs.getInputTF()) {
235 case Key::INPUT_TF_SRGB:
236 fs << R"__SHADER__(
237 float EOTF_sRGB(float srgb) {
238 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
239 }
240
241 vec3 EOTF_sRGB(const vec3 srgb) {
242 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
243 }
244
245 vec3 EOTF(const vec3 srgb) {
246 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
247 }
248 )__SHADER__";
249 break;
250 case Key::INPUT_TF_ST2084:
251 fs << R"__SHADER__(
252 vec3 EOTF(const highp vec3 color) {
253 const highp float m1 = (2610.0 / 4096.0) / 4.0;
254 const highp float m2 = (2523.0 / 4096.0) * 128.0;
255 const highp float c1 = (3424.0 / 4096.0);
256 const highp float c2 = (2413.0 / 4096.0) * 32.0;
257 const highp float c3 = (2392.0 / 4096.0) * 32.0;
258
Siddharth Kapoorac1f7c92018-12-05 20:29:06 +0800259 highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700260 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
261 return pow(tmp, 1.0 / vec3(m1));
262 }
263 )__SHADER__";
264 break;
265 case Key::INPUT_TF_HLG:
266 fs << R"__SHADER__(
267 highp float EOTF_channel(const highp float channel) {
268 const highp float a = 0.17883277;
269 const highp float b = 0.28466892;
270 const highp float c = 0.55991073;
271 return channel <= 0.5 ? channel * channel / 3.0 :
272 (exp((channel - c) / a) + b) / 12.0;
273 }
274
275 vec3 EOTF(const highp vec3 color) {
276 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
277 EOTF_channel(color.b));
278 }
279 )__SHADER__";
280 break;
281 default:
282 fs << R"__SHADER__(
283 vec3 EOTF(const vec3 linear) {
284 return linear;
285 }
286 )__SHADER__";
287 break;
288 }
289}
290
Peiyong Lin53f320e2018-04-23 17:31:06 -0700291void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
292 // Convert relative light to absolute light.
293 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700294 case Key::INPUT_TF_ST2084:
295 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700296 highp vec3 ScaleLuminance(highp vec3 color) {
297 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700298 }
299 )__SHADER__";
300 break;
301 case Key::INPUT_TF_HLG:
302 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700303 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700304 // The formula is:
305 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700306 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
307 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700308 }
309 )__SHADER__";
310 break;
311 default:
312 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700313 highp vec3 ScaleLuminance(highp vec3 color) {
314 return color * displayMaxLuminance;
315 }
316 )__SHADER__";
317 break;
318 }
319
320 // Tone map absolute light to display luminance range.
321 switch (needs.getInputTF()) {
322 case Key::INPUT_TF_ST2084:
323 case Key::INPUT_TF_HLG:
324 switch (needs.getOutputTF()) {
325 case Key::OUTPUT_TF_HLG:
326 // Right now when mixed PQ and HLG contents are presented,
327 // HLG content will always be converted to PQ. However, for
328 // completeness, we simply clamp the value to [0.0, 1000.0].
329 fs << R"__SHADER__(
330 highp vec3 ToneMap(highp vec3 color) {
331 return clamp(color, 0.0, 1000.0);
332 }
333 )__SHADER__";
334 break;
335 case Key::OUTPUT_TF_ST2084:
336 fs << R"__SHADER__(
337 highp vec3 ToneMap(highp vec3 color) {
338 return color;
339 }
340 )__SHADER__";
341 break;
342 default:
343 fs << R"__SHADER__(
344 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800345 float maxMasteringLumi = maxMasteringLuminance;
346 float maxContentLumi = maxContentLuminance;
347 float maxInLumi = min(maxMasteringLumi, maxContentLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700348 float maxOutLumi = displayMaxLuminance;
349
350 float nits = color.y;
351
352 // clamp to max input luminance
353 nits = clamp(nits, 0.0, maxInLumi);
354
355 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
356 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700357 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700358 } else {
359 // three control points
360 const float x0 = 10.0;
361 const float y0 = 17.0;
362 float x1 = maxOutLumi * 0.75;
363 float y1 = x1;
364 float x2 = x1 + (maxInLumi - x1) / 2.0;
365 float y2 = y1 + (maxOutLumi - y1) * 0.75;
366
367 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700368 float h12 = x2 - x1;
369 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700370 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700371 float m1 = (y2 - y1) / h12;
372 float m3 = (maxOutLumi - y2) / h23;
373 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700374
375 if (nits < x0) {
376 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700377 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700378 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700379 } else if (nits < x1) {
380 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700381 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700382 nits = y0 + (nits - x0) * slope;
383 } else if (nits < x2) {
384 // scale [x1, x2] to [y1, y2] using Hermite interp
385 float t = (nits - x1) / h12;
386 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
387 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
388 } else {
389 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
390 float t = (nits - x2) / h23;
391 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
392 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
393 }
394 }
395
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700396 // color.y is greater than x0 and is thus non-zero
397 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700398 }
399 )__SHADER__";
400 break;
401 }
402 break;
403 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700404 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700405 fs << R"__SHADER__(
406 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700407 const float maxOutLumi = 3000.0;
408
409 const float x0 = 5.0;
410 const float y0 = 2.5;
411 float x1 = displayMaxLuminance * 0.7;
412 float y1 = maxOutLumi * 0.15;
413 float x2 = displayMaxLuminance * 0.9;
414 float y2 = maxOutLumi * 0.45;
415 float x3 = displayMaxLuminance;
416 float y3 = maxOutLumi;
417
418 float c1 = y1 / 3.0;
419 float c2 = y2 / 2.0;
420 float c3 = y3 / 1.5;
421
422 float nits = color.y;
423
424 float scale;
425 if (nits <= x0) {
426 // scale [0.0, x0] to [0.0, y0] linearly
427 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700428 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700429 } else if (nits <= x1) {
430 // scale [x0, x1] to [y0, y1] using a curve
431 float t = (nits - x0) / (x1 - x0);
432 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
433 } else if (nits <= x2) {
434 // scale [x1, x2] to [y1, y2] using a curve
435 float t = (nits - x1) / (x2 - x1);
436 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
437 } else {
438 // scale [x2, x3] to [y2, y3] using a curve
439 float t = (nits - x2) / (x3 - x2);
440 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
441 }
442
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700443 // color.y is greater than x0 and is thus non-zero
444 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700445 }
446 )__SHADER__";
447 break;
448 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700449
450 // convert absolute light to relative light.
451 switch (needs.getOutputTF()) {
452 case Key::OUTPUT_TF_ST2084:
453 fs << R"__SHADER__(
454 highp vec3 NormalizeLuminance(highp vec3 color) {
455 return color / 10000.0;
456 }
457 )__SHADER__";
458 break;
459 case Key::OUTPUT_TF_HLG:
460 fs << R"__SHADER__(
461 highp vec3 NormalizeLuminance(highp vec3 color) {
462 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
463 }
464 )__SHADER__";
465 break;
466 default:
467 fs << R"__SHADER__(
468 highp vec3 NormalizeLuminance(highp vec3 color) {
469 return color / displayMaxLuminance;
470 }
471 )__SHADER__";
472 break;
473 }
474}
475
476// Generate OOTF that modifies the relative scence light to relative display light.
477void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
478 if (!needs.needsToneMapping()) {
479 fs << R"__SHADER__(
480 highp vec3 OOTF(const highp vec3 color) {
481 return color;
482 }
483 )__SHADER__";
484 } else {
485 generateToneMappingProcess(fs, needs);
486 fs << R"__SHADER__(
487 highp vec3 OOTF(const highp vec3 color) {
488 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
489 }
490 )__SHADER__";
491 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700492}
493
494// Generate OETF that converts relative display light to signal values,
495// both normalized to [0, 1]
496void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
497 switch (needs.getOutputTF()) {
498 case Key::OUTPUT_TF_SRGB:
499 fs << R"__SHADER__(
500 float OETF_sRGB(const float linear) {
501 return linear <= 0.0031308 ?
502 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
503 }
504
505 vec3 OETF_sRGB(const vec3 linear) {
506 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
507 }
508
509 vec3 OETF(const vec3 linear) {
510 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
511 }
512 )__SHADER__";
513 break;
514 case Key::OUTPUT_TF_ST2084:
515 fs << R"__SHADER__(
516 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700517 const highp float m1 = (2610.0 / 4096.0) / 4.0;
518 const highp float m2 = (2523.0 / 4096.0) * 128.0;
519 const highp float c1 = (3424.0 / 4096.0);
520 const highp float c2 = (2413.0 / 4096.0) * 32.0;
521 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700522
Peiyong Lin834be492018-05-18 15:12:55 -0700523 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700524 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
525 return pow(tmp, vec3(m2));
526 }
527 )__SHADER__";
528 break;
529 case Key::OUTPUT_TF_HLG:
530 fs << R"__SHADER__(
531 highp float OETF_channel(const highp float channel) {
532 const highp float a = 0.17883277;
533 const highp float b = 0.28466892;
534 const highp float c = 0.55991073;
535 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
536 a * log(12.0 * channel - b) + c;
537 }
538
539 vec3 OETF(const highp vec3 color) {
540 return vec3(OETF_channel(color.r), OETF_channel(color.g),
541 OETF_channel(color.b));
542 }
543 )__SHADER__";
544 break;
545 default:
546 fs << R"__SHADER__(
547 vec3 OETF(const vec3 linear) {
548 return linear;
549 }
550 )__SHADER__";
551 break;
552 }
553}
554
Mathias Agopian3f844832013-08-07 21:24:32 -0700555String8 ProgramCache::generateVertexShader(const Key& needs) {
556 Formatter vs;
Vishnu Nairf19544f2020-02-03 11:23:26 -0800557 if (needs.hasTextureCoords()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800558 vs << "attribute vec4 texCoords;"
559 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700560 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700561 if (needs.hasRoundedCorners()) {
562 vs << "attribute lowp vec4 cropCoords;";
563 vs << "varying lowp vec2 outCropCoords;";
564 }
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800565 if (needs.drawShadows()) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800566 vs << "attribute lowp vec4 shadowColor;";
567 vs << "varying lowp vec4 outShadowColor;";
568 vs << "attribute lowp vec4 shadowParams;";
569 vs << "varying lowp vec3 outShadowParams;";
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800570 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700571 vs << "attribute vec4 position;"
572 << "uniform mat4 projection;"
573 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800574 << "void main(void) {" << indent << "gl_Position = projection * position;";
Vishnu Nairf19544f2020-02-03 11:23:26 -0800575 if (needs.hasTextureCoords()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700576 vs << "outTexCoords = (texture * texCoords).st;";
577 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700578 if (needs.hasRoundedCorners()) {
579 vs << "outCropCoords = cropCoords.st;";
580 }
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800581 if (needs.drawShadows()) {
582 vs << "outShadowColor = shadowColor;";
583 vs << "outShadowParams = shadowParams.xyz;";
584 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700585 vs << dedent << "}";
586 return vs.getString();
587}
588
589String8 ProgramCache::generateFragmentShader(const Key& needs) {
590 Formatter fs;
591 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
592 fs << "#extension GL_OES_EGL_image_external : require";
593 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700594
595 // default precision is required-ish in fragment shaders
596 fs << "precision mediump float;";
597
Mathias Agopian3f844832013-08-07 21:24:32 -0700598 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800599 fs << "uniform samplerExternalOES sampler;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700600 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800601 fs << "uniform sampler2D sampler;";
602 }
603
604 if (needs.hasTextureCoords()) {
605 fs << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700606 }
607
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700608 if (needs.hasRoundedCorners()) {
609 // Rounded corners implementation using a signed distance function.
610 fs << R"__SHADER__(
611 uniform float cornerRadius;
612 uniform vec2 cropCenter;
613 varying vec2 outCropCoords;
614
615 /**
616 * This function takes the current crop coordinates and calculates an alpha value based
617 * on the corner radius and distance from the crop center.
618 */
619 float applyCornerRadius(vec2 cropCoords)
620 {
621 vec2 position = cropCoords - cropCenter;
Alec Mouri0d0e16e2019-07-25 15:22:29 -0700622 // Scale down the dist vector here, as otherwise large corner
623 // radii can cause floating point issues when computing the norm
624 vec2 dist = (abs(position) - cropCenter + vec2(cornerRadius)) / 16.0;
625 // Once we've found the norm, then scale back up.
626 float plane = length(max(dist, vec2(0.0))) * 16.0;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700627 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
628 }
629 )__SHADER__";
630 }
631
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800632 if (needs.drawShadows()) {
633 fs << R"__SHADER__(
Vishnu Nairf19544f2020-02-03 11:23:26 -0800634 varying lowp vec4 outShadowColor;
635 varying lowp vec3 outShadowParams;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800636
637 /**
638 * Returns the shadow color.
639 */
640 vec4 getShadowColor()
641 {
Vishnu Nairf19544f2020-02-03 11:23:26 -0800642 lowp float d = length(outShadowParams.xy);
643 vec2 uv = vec2(outShadowParams.z * (1.0 - d), 0.5);
644 lowp float factor = texture2D(sampler, uv).a;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800645 return outShadowColor * factor;
646 }
647 )__SHADER__";
648 }
649
chaviw13fdc492017-06-27 12:40:18 -0700650 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700651 fs << "uniform vec4 color;";
652 }
chaviw13fdc492017-06-27 12:40:18 -0700653
Chia-I Wu131d3762018-01-11 14:35:27 -0800654 if (needs.isY410BT2020()) {
655 fs << R"__SHADER__(
656 vec3 convertY410BT2020(const vec3 color) {
657 const vec3 offset = vec3(0.0625, 0.5, 0.5);
658 const mat3 transform = mat3(
659 vec3(1.1678, 1.1678, 1.1678),
660 vec3( 0.0, -0.1878, 2.1481),
661 vec3(1.6836, -0.6523, 0.0));
662 // Y is in G, U is in R, and V is in B
663 return clamp(transform * (color.grb - offset), 0.0, 1.0);
664 }
665 )__SHADER__";
666 }
667
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800668 if (needs.hasTransformMatrix() ||
669 (needs.getInputTF() != needs.getOutputTF()) ||
670 needs.hasDisplayColorMatrix()) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700671 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700672 fs << "uniform float displayMaxLuminance;";
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800673 fs << "uniform float maxMasteringLuminance;";
674 fs << "uniform float maxContentLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700675 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700676
Peiyong Lina296b0c2018-04-30 16:55:29 -0700677 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700678 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700679 fs << R"__SHADER__(
680 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700681 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700682 }
683 )__SHADER__";
684 } else {
685 fs << R"__SHADER__(
686 highp vec3 InputTransform(const highp vec3 color) {
687 return color;
688 }
689 )__SHADER__";
690 }
691
692 // the transformation from a wider colorspace to a narrower one can
693 // result in >1.0 or <0.0 pixel values
694 if (needs.hasOutputTransformMatrix()) {
695 fs << "uniform mat4 outputTransformMatrix;";
696 fs << R"__SHADER__(
697 highp vec3 OutputTransform(const highp vec3 color) {
698 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
699 }
700 )__SHADER__";
701 } else {
702 fs << R"__SHADER__(
703 highp vec3 OutputTransform(const highp vec3 color) {
704 return clamp(color, 0.0, 1.0);
705 }
706 )__SHADER__";
707 }
708
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800709 if (needs.hasDisplayColorMatrix()) {
710 fs << "uniform mat4 displayColorMatrix;";
711 fs << R"__SHADER__(
712 highp vec3 DisplayColorMatrix(const highp vec3 color) {
713 return clamp(vec3(displayColorMatrix * vec4(color, 1.0)), 0.0, 1.0);
714 }
715 )__SHADER__";
716 } else {
717 fs << R"__SHADER__(
718 highp vec3 DisplayColorMatrix(const highp vec3 color) {
719 return color;
720 }
721 )__SHADER__";
722 }
723
Peiyong Lin2542cb02018-04-30 17:29:53 -0700724 generateEOTF(fs, needs);
725 generateOOTF(fs, needs);
726 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700727 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800728
Mathias Agopian3f844832013-08-07 21:24:32 -0700729 fs << "void main(void) {" << indent;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800730 if (needs.drawShadows()) {
731 fs << "gl_FragColor = getShadowColor();";
Mathias Agopian3f844832013-08-07 21:24:32 -0700732 } else {
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800733 if (needs.isTexturing()) {
734 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
735 if (needs.isY410BT2020()) {
736 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
737 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700738 } else {
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800739 fs << "gl_FragColor.rgb = color.rgb;";
740 fs << "gl_FragColor.a = 1.0;";
741 }
742 if (needs.isOpaque()) {
743 fs << "gl_FragColor.a = 1.0;";
744 }
745 if (needs.hasAlpha()) {
746 // modulate the current alpha value with alpha set
747 if (needs.isPremultiplied()) {
748 // ... and the color too if we're premultiplied
749 fs << "gl_FragColor *= color.a;";
750 } else {
751 fs << "gl_FragColor.a *= color.a;";
752 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700753 }
754 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700755
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800756 if (needs.hasTransformMatrix() ||
757 (needs.getInputTF() != needs.getOutputTF()) ||
758 needs.hasDisplayColorMatrix()) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700759 if (!needs.isOpaque() && needs.isPremultiplied()) {
760 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700761 // avoid divide by 0 by adding 0.5/256 to the alpha channel
762 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700763 }
Peiyong Lin46080ef2018-10-26 18:43:14 -0700764 fs << "gl_FragColor.rgb = "
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800765 "DisplayColorMatrix(OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb))))));";
766
Mathias Agopianff2ed702013-09-01 21:36:12 -0700767 if (!needs.isOpaque() && needs.isPremultiplied()) {
768 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700769 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700770 }
771 }
772
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700773 if (needs.hasRoundedCorners()) {
774 if (needs.isPremultiplied()) {
775 fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
776 } else {
777 fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
778 }
779 }
780
Mathias Agopian3f844832013-08-07 21:24:32 -0700781 fs << dedent << "}";
782 return fs.getString();
783}
784
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700785std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700786 ATRACE_CALL();
787
Mathias Agopian3f844832013-08-07 21:24:32 -0700788 // vertex shader
789 String8 vs = generateVertexShader(needs);
790
791 // fragment shader
792 String8 fs = generateFragmentShader(needs);
793
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700794 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700795}
796
Peiyong Linfb530cf2018-12-15 05:07:38 +0000797void ProgramCache::useProgram(EGLContext context, const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700798 // generate the key for the shader based on the description
799 Key needs(computeKey(description));
800
Chia-I Wub027f802017-11-29 14:00:52 -0800801 // look-up the program in the cache
Peiyong Linfb530cf2018-12-15 05:07:38 +0000802 auto& cache = mCaches[context];
803 auto it = cache.find(needs);
804 if (it == cache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700805 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700806 nsecs_t time = systemTime();
Peiyong Linfb530cf2018-12-15 05:07:38 +0000807 it = cache.emplace(needs, generateProgram(needs)).first;
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700808 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700809
Peiyong Linfb530cf2018-12-15 05:07:38 +0000810 ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)",
811 context, needs.mKey, uint32_t(ns2ms(time)), cache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700812 }
813
814 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700815 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700816 if (program->isValid()) {
817 program->use();
818 program->setUniforms(description);
819 }
820}
821
Peiyong Lin46080ef2018-10-26 18:43:14 -0700822} // namespace gl
823} // namespace renderengine
824} // namespace android