blob: 41870d2158b9d576e07f09488d10501b42e72753 [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
Bo Hu78fb96f2018-12-14 20:50:18 +000080void ProgramCache::primeCache(bool useColorManagement) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070081 uint32_t shaderCount = 0;
Lucas Dupin1b6531c2018-07-05 17:18:21 -070082 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK
83 | Key::ROUNDED_CORNERS_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070084 // Prime the cache for all combinations of the above masks,
85 // leaving off the experimental color matrix mask options.
86
87 nsecs_t timeBefore = systemTime();
88 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
89 Key shaderKey;
90 shaderKey.set(keyMask, keyVal);
91 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080092 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070093 continue;
94 }
Bo Hu78fb96f2018-12-14 20:50:18 +000095 if (mCache.count(shaderKey) == 0) {
96 mCache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -070097 shaderCount++;
98 }
99 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700100
101 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700102 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700103 Key shaderKey;
104 shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK |
105 Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK,
106 Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON |
107 Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB);
108 for (int i = 0; i < 4; i++) {
109 shaderKey.set(Key::OPACITY_MASK,
110 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
111 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Bo Hu78fb96f2018-12-14 20:50:18 +0000112 if (mCache.count(shaderKey) == 0) {
113 mCache.emplace(shaderKey, generateProgram(shaderKey));
Chia-I Wu93e14df2018-06-04 10:10:17 -0700114 shaderCount++;
115 }
116 }
117 }
118
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700119 nsecs_t timeAfter = systemTime();
120 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
121 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
122}
123
Mathias Agopian3f844832013-08-07 21:24:32 -0700124ProgramCache::Key ProgramCache::computeKey(const Description& description) {
125 Key needs;
126 needs.set(Key::TEXTURE_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700127 !description.textureEnabled
Chia-I Wub027f802017-11-29 14:00:52 -0800128 ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700129 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Chia-I Wub027f802017-11-29 14:00:52 -0800130 ? Key::TEXTURE_EXT
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700131 : description.texture.getTextureTarget() == GL_TEXTURE_2D
Chia-I Wub027f802017-11-29 14:00:52 -0800132 ? Key::TEXTURE_2D
133 : Key::TEXTURE_OFF)
Peiyong Lin46080ef2018-10-26 18:43:14 -0700134 .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800135 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700136 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800137 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700138 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700139 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700140 description.hasInputTransformMatrix()
141 ? Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700142 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Peiyong Lin46080ef2018-10-26 18:43:14 -0700143 description.hasOutputTransformMatrix() || description.hasColorMatrix()
144 ? Key::OUTPUT_TRANSFORM_MATRIX_ON
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700145 : Key::OUTPUT_TRANSFORM_MATRIX_OFF)
146 .set(Key::ROUNDED_CORNERS_MASK,
147 description.cornerRadius > 0
148 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800149
Chia-I Wu131d3762018-01-11 14:35:27 -0800150 needs.set(Key::Y410_BT2020_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700151 description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800152
Peiyong Lina296b0c2018-04-30 16:55:29 -0700153 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700154 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800155 case Description::TransferFunction::LINEAR:
156 default:
157 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
158 break;
159 case Description::TransferFunction::SRGB:
160 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
161 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800162 case Description::TransferFunction::ST2084:
163 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
164 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700165 case Description::TransferFunction::HLG:
166 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
167 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800168 }
169
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700170 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800171 case Description::TransferFunction::LINEAR:
172 default:
173 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
174 break;
175 case Description::TransferFunction::SRGB:
176 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
177 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800178 case Description::TransferFunction::ST2084:
179 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
180 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700181 case Description::TransferFunction::HLG:
182 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
183 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800184 }
185 }
186
Mathias Agopian3f844832013-08-07 21:24:32 -0700187 return needs;
188}
189
Peiyong Lin2542cb02018-04-30 17:29:53 -0700190// Generate EOTF that converts signal values to relative display light,
191// both normalized to [0, 1].
192void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
193 switch (needs.getInputTF()) {
194 case Key::INPUT_TF_SRGB:
195 fs << R"__SHADER__(
196 float EOTF_sRGB(float srgb) {
197 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
198 }
199
200 vec3 EOTF_sRGB(const vec3 srgb) {
201 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
202 }
203
204 vec3 EOTF(const vec3 srgb) {
205 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
206 }
207 )__SHADER__";
208 break;
209 case Key::INPUT_TF_ST2084:
210 fs << R"__SHADER__(
211 vec3 EOTF(const highp vec3 color) {
212 const highp float m1 = (2610.0 / 4096.0) / 4.0;
213 const highp float m2 = (2523.0 / 4096.0) * 128.0;
214 const highp float c1 = (3424.0 / 4096.0);
215 const highp float c2 = (2413.0 / 4096.0) * 32.0;
216 const highp float c3 = (2392.0 / 4096.0) * 32.0;
217
Siddharth Kapoorac1f7c92018-12-05 20:29:06 +0800218 highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700219 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
220 return pow(tmp, 1.0 / vec3(m1));
221 }
222 )__SHADER__";
223 break;
224 case Key::INPUT_TF_HLG:
225 fs << R"__SHADER__(
226 highp float EOTF_channel(const highp float channel) {
227 const highp float a = 0.17883277;
228 const highp float b = 0.28466892;
229 const highp float c = 0.55991073;
230 return channel <= 0.5 ? channel * channel / 3.0 :
231 (exp((channel - c) / a) + b) / 12.0;
232 }
233
234 vec3 EOTF(const highp vec3 color) {
235 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
236 EOTF_channel(color.b));
237 }
238 )__SHADER__";
239 break;
240 default:
241 fs << R"__SHADER__(
242 vec3 EOTF(const vec3 linear) {
243 return linear;
244 }
245 )__SHADER__";
246 break;
247 }
248}
249
Peiyong Lin53f320e2018-04-23 17:31:06 -0700250void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
251 // Convert relative light to absolute light.
252 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700253 case Key::INPUT_TF_ST2084:
254 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700255 highp vec3 ScaleLuminance(highp vec3 color) {
256 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700257 }
258 )__SHADER__";
259 break;
260 case Key::INPUT_TF_HLG:
261 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700262 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700263 // The formula is:
264 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700265 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
266 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700267 }
268 )__SHADER__";
269 break;
270 default:
271 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700272 highp vec3 ScaleLuminance(highp vec3 color) {
273 return color * displayMaxLuminance;
274 }
275 )__SHADER__";
276 break;
277 }
278
279 // Tone map absolute light to display luminance range.
280 switch (needs.getInputTF()) {
281 case Key::INPUT_TF_ST2084:
282 case Key::INPUT_TF_HLG:
283 switch (needs.getOutputTF()) {
284 case Key::OUTPUT_TF_HLG:
285 // Right now when mixed PQ and HLG contents are presented,
286 // HLG content will always be converted to PQ. However, for
287 // completeness, we simply clamp the value to [0.0, 1000.0].
288 fs << R"__SHADER__(
289 highp vec3 ToneMap(highp vec3 color) {
290 return clamp(color, 0.0, 1000.0);
291 }
292 )__SHADER__";
293 break;
294 case Key::OUTPUT_TF_ST2084:
295 fs << R"__SHADER__(
296 highp vec3 ToneMap(highp vec3 color) {
297 return color;
298 }
299 )__SHADER__";
300 break;
301 default:
302 fs << R"__SHADER__(
303 highp vec3 ToneMap(highp vec3 color) {
304 const float maxMasteringLumi = 1000.0;
305 const float maxContentLumi = 1000.0;
306 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
307 float maxOutLumi = displayMaxLuminance;
308
309 float nits = color.y;
310
311 // clamp to max input luminance
312 nits = clamp(nits, 0.0, maxInLumi);
313
314 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
315 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700316 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700317 } else {
318 // three control points
319 const float x0 = 10.0;
320 const float y0 = 17.0;
321 float x1 = maxOutLumi * 0.75;
322 float y1 = x1;
323 float x2 = x1 + (maxInLumi - x1) / 2.0;
324 float y2 = y1 + (maxOutLumi - y1) * 0.75;
325
326 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700327 float h12 = x2 - x1;
328 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700329 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700330 float m1 = (y2 - y1) / h12;
331 float m3 = (maxOutLumi - y2) / h23;
332 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700333
334 if (nits < x0) {
335 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700336 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700337 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700338 } else if (nits < x1) {
339 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700340 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700341 nits = y0 + (nits - x0) * slope;
342 } else if (nits < x2) {
343 // scale [x1, x2] to [y1, y2] using Hermite interp
344 float t = (nits - x1) / h12;
345 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
346 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
347 } else {
348 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
349 float t = (nits - x2) / h23;
350 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
351 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
352 }
353 }
354
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700355 // color.y is greater than x0 and is thus non-zero
356 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700357 }
358 )__SHADER__";
359 break;
360 }
361 break;
362 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700363 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700364 fs << R"__SHADER__(
365 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700366 const float maxOutLumi = 3000.0;
367
368 const float x0 = 5.0;
369 const float y0 = 2.5;
370 float x1 = displayMaxLuminance * 0.7;
371 float y1 = maxOutLumi * 0.15;
372 float x2 = displayMaxLuminance * 0.9;
373 float y2 = maxOutLumi * 0.45;
374 float x3 = displayMaxLuminance;
375 float y3 = maxOutLumi;
376
377 float c1 = y1 / 3.0;
378 float c2 = y2 / 2.0;
379 float c3 = y3 / 1.5;
380
381 float nits = color.y;
382
383 float scale;
384 if (nits <= x0) {
385 // scale [0.0, x0] to [0.0, y0] linearly
386 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700387 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700388 } else if (nits <= x1) {
389 // scale [x0, x1] to [y0, y1] using a curve
390 float t = (nits - x0) / (x1 - x0);
391 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
392 } else if (nits <= x2) {
393 // scale [x1, x2] to [y1, y2] using a curve
394 float t = (nits - x1) / (x2 - x1);
395 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
396 } else {
397 // scale [x2, x3] to [y2, y3] using a curve
398 float t = (nits - x2) / (x3 - x2);
399 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
400 }
401
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700402 // color.y is greater than x0 and is thus non-zero
403 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700404 }
405 )__SHADER__";
406 break;
407 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700408
409 // convert absolute light to relative light.
410 switch (needs.getOutputTF()) {
411 case Key::OUTPUT_TF_ST2084:
412 fs << R"__SHADER__(
413 highp vec3 NormalizeLuminance(highp vec3 color) {
414 return color / 10000.0;
415 }
416 )__SHADER__";
417 break;
418 case Key::OUTPUT_TF_HLG:
419 fs << R"__SHADER__(
420 highp vec3 NormalizeLuminance(highp vec3 color) {
421 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
422 }
423 )__SHADER__";
424 break;
425 default:
426 fs << R"__SHADER__(
427 highp vec3 NormalizeLuminance(highp vec3 color) {
428 return color / displayMaxLuminance;
429 }
430 )__SHADER__";
431 break;
432 }
433}
434
435// Generate OOTF that modifies the relative scence light to relative display light.
436void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
437 if (!needs.needsToneMapping()) {
438 fs << R"__SHADER__(
439 highp vec3 OOTF(const highp vec3 color) {
440 return color;
441 }
442 )__SHADER__";
443 } else {
444 generateToneMappingProcess(fs, needs);
445 fs << R"__SHADER__(
446 highp vec3 OOTF(const highp vec3 color) {
447 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
448 }
449 )__SHADER__";
450 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700451}
452
453// Generate OETF that converts relative display light to signal values,
454// both normalized to [0, 1]
455void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
456 switch (needs.getOutputTF()) {
457 case Key::OUTPUT_TF_SRGB:
458 fs << R"__SHADER__(
459 float OETF_sRGB(const float linear) {
460 return linear <= 0.0031308 ?
461 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
462 }
463
464 vec3 OETF_sRGB(const vec3 linear) {
465 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
466 }
467
468 vec3 OETF(const vec3 linear) {
469 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
470 }
471 )__SHADER__";
472 break;
473 case Key::OUTPUT_TF_ST2084:
474 fs << R"__SHADER__(
475 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700476 const highp float m1 = (2610.0 / 4096.0) / 4.0;
477 const highp float m2 = (2523.0 / 4096.0) * 128.0;
478 const highp float c1 = (3424.0 / 4096.0);
479 const highp float c2 = (2413.0 / 4096.0) * 32.0;
480 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700481
Peiyong Lin834be492018-05-18 15:12:55 -0700482 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700483 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
484 return pow(tmp, vec3(m2));
485 }
486 )__SHADER__";
487 break;
488 case Key::OUTPUT_TF_HLG:
489 fs << R"__SHADER__(
490 highp float OETF_channel(const highp float channel) {
491 const highp float a = 0.17883277;
492 const highp float b = 0.28466892;
493 const highp float c = 0.55991073;
494 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
495 a * log(12.0 * channel - b) + c;
496 }
497
498 vec3 OETF(const highp vec3 color) {
499 return vec3(OETF_channel(color.r), OETF_channel(color.g),
500 OETF_channel(color.b));
501 }
502 )__SHADER__";
503 break;
504 default:
505 fs << R"__SHADER__(
506 vec3 OETF(const vec3 linear) {
507 return linear;
508 }
509 )__SHADER__";
510 break;
511 }
512}
513
Mathias Agopian3f844832013-08-07 21:24:32 -0700514String8 ProgramCache::generateVertexShader(const Key& needs) {
515 Formatter vs;
516 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800517 vs << "attribute vec4 texCoords;"
518 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700519 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700520 if (needs.hasRoundedCorners()) {
521 vs << "attribute lowp vec4 cropCoords;";
522 vs << "varying lowp vec2 outCropCoords;";
523 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700524 vs << "attribute vec4 position;"
525 << "uniform mat4 projection;"
526 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800527 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700528 if (needs.isTexturing()) {
529 vs << "outTexCoords = (texture * texCoords).st;";
530 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700531 if (needs.hasRoundedCorners()) {
532 vs << "outCropCoords = cropCoords.st;";
533 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700534 vs << dedent << "}";
535 return vs.getString();
536}
537
538String8 ProgramCache::generateFragmentShader(const Key& needs) {
539 Formatter fs;
540 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
541 fs << "#extension GL_OES_EGL_image_external : require";
542 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700543
544 // default precision is required-ish in fragment shaders
545 fs << "precision mediump float;";
546
Mathias Agopian3f844832013-08-07 21:24:32 -0700547 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
548 fs << "uniform samplerExternalOES sampler;"
549 << "varying vec2 outTexCoords;";
550 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
551 fs << "uniform sampler2D sampler;"
552 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700553 }
554
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700555 if (needs.hasRoundedCorners()) {
556 // Rounded corners implementation using a signed distance function.
557 fs << R"__SHADER__(
558 uniform float cornerRadius;
559 uniform vec2 cropCenter;
560 varying vec2 outCropCoords;
561
562 /**
563 * This function takes the current crop coordinates and calculates an alpha value based
564 * on the corner radius and distance from the crop center.
565 */
566 float applyCornerRadius(vec2 cropCoords)
567 {
568 vec2 position = cropCoords - cropCenter;
569 vec2 dist = abs(position) + vec2(cornerRadius) - cropCenter;
570 float plane = length(max(dist, vec2(0.0)));
571 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
572 }
573 )__SHADER__";
574 }
575
chaviw13fdc492017-06-27 12:40:18 -0700576 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700577 fs << "uniform vec4 color;";
578 }
chaviw13fdc492017-06-27 12:40:18 -0700579
Chia-I Wu131d3762018-01-11 14:35:27 -0800580 if (needs.isY410BT2020()) {
581 fs << R"__SHADER__(
582 vec3 convertY410BT2020(const vec3 color) {
583 const vec3 offset = vec3(0.0625, 0.5, 0.5);
584 const mat3 transform = mat3(
585 vec3(1.1678, 1.1678, 1.1678),
586 vec3( 0.0, -0.1878, 2.1481),
587 vec3(1.6836, -0.6523, 0.0));
588 // Y is in G, U is in R, and V is in B
589 return clamp(transform * (color.grb - offset), 0.0, 1.0);
590 }
591 )__SHADER__";
592 }
593
Peiyong Lina296b0c2018-04-30 16:55:29 -0700594 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700595 // Currently, display maximum luminance is needed when doing tone mapping.
596 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700597 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700598 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700599
Peiyong Lina296b0c2018-04-30 16:55:29 -0700600 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700601 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700602 fs << R"__SHADER__(
603 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700604 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700605 }
606 )__SHADER__";
607 } else {
608 fs << R"__SHADER__(
609 highp vec3 InputTransform(const highp vec3 color) {
610 return color;
611 }
612 )__SHADER__";
613 }
614
615 // the transformation from a wider colorspace to a narrower one can
616 // result in >1.0 or <0.0 pixel values
617 if (needs.hasOutputTransformMatrix()) {
618 fs << "uniform mat4 outputTransformMatrix;";
619 fs << R"__SHADER__(
620 highp vec3 OutputTransform(const highp vec3 color) {
621 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
622 }
623 )__SHADER__";
624 } else {
625 fs << R"__SHADER__(
626 highp vec3 OutputTransform(const highp vec3 color) {
627 return clamp(color, 0.0, 1.0);
628 }
629 )__SHADER__";
630 }
631
Peiyong Lin2542cb02018-04-30 17:29:53 -0700632 generateEOTF(fs, needs);
633 generateOOTF(fs, needs);
634 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700635 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800636
Mathias Agopian3f844832013-08-07 21:24:32 -0700637 fs << "void main(void) {" << indent;
638 if (needs.isTexturing()) {
639 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800640 if (needs.isY410BT2020()) {
641 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
642 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700643 } else {
chaviw13fdc492017-06-27 12:40:18 -0700644 fs << "gl_FragColor.rgb = color.rgb;";
645 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700646 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700647 if (needs.isOpaque()) {
648 fs << "gl_FragColor.a = 1.0;";
649 }
chaviw13fdc492017-06-27 12:40:18 -0700650 if (needs.hasAlpha()) {
651 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700652 if (needs.isPremultiplied()) {
653 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700654 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700655 } else {
chaviw13fdc492017-06-27 12:40:18 -0700656 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700657 }
658 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700659
Peiyong Lina296b0c2018-04-30 16:55:29 -0700660 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700661 if (!needs.isOpaque() && needs.isPremultiplied()) {
662 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700663 // avoid divide by 0 by adding 0.5/256 to the alpha channel
664 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700665 }
Peiyong Lin46080ef2018-10-26 18:43:14 -0700666 fs << "gl_FragColor.rgb = "
667 "OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700668 if (!needs.isOpaque() && needs.isPremultiplied()) {
669 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700670 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700671 }
672 }
673
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700674 if (needs.hasRoundedCorners()) {
675 if (needs.isPremultiplied()) {
676 fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
677 } else {
678 fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
679 }
680 }
681
Mathias Agopian3f844832013-08-07 21:24:32 -0700682 fs << dedent << "}";
683 return fs.getString();
684}
685
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700686std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700687 ATRACE_CALL();
688
Mathias Agopian3f844832013-08-07 21:24:32 -0700689 // vertex shader
690 String8 vs = generateVertexShader(needs);
691
692 // fragment shader
693 String8 fs = generateFragmentShader(needs);
694
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700695 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700696}
697
Bo Hu78fb96f2018-12-14 20:50:18 +0000698void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700699 // generate the key for the shader based on the description
700 Key needs(computeKey(description));
701
Chia-I Wub027f802017-11-29 14:00:52 -0800702 // look-up the program in the cache
Bo Hu78fb96f2018-12-14 20:50:18 +0000703 auto it = mCache.find(needs);
704 if (it == mCache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700705 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700706 nsecs_t time = systemTime();
Bo Hu78fb96f2018-12-14 20:50:18 +0000707 it = mCache.emplace(needs, generateProgram(needs)).first;
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700708 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700709
Bo Hu78fb96f2018-12-14 20:50:18 +0000710 ALOGV(">>> generated new program: needs=%08X, time=%u ms (%zu programs)", needs.mKey,
711 uint32_t(ns2ms(time)), mCache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700712 }
713
714 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700715 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700716 if (program->isValid()) {
717 program->use();
718 program->setUniforms(description);
719 }
720}
721
Peiyong Lin46080ef2018-10-26 18:43:14 -0700722} // namespace gl
723} // namespace renderengine
724} // namespace android