blob: bf2354dbfd3b12d08f8ff4297cdf4bdfc11e3fc5 [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
Peiyong Linfb530cf2018-12-15 05:07:38 +000080void ProgramCache::primeCache(EGLContext context, bool useColorManagement) {
81 auto& cache = mCaches[context];
Riley Andrewsa51fafc2014-09-29 13:29:40 -070082 uint32_t shaderCount = 0;
Lucas Dupin1b6531c2018-07-05 17:18:21 -070083 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK
84 | Key::ROUNDED_CORNERS_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070085 // Prime the cache for all combinations of the above masks,
86 // leaving off the experimental color matrix mask options.
87
88 nsecs_t timeBefore = systemTime();
89 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
90 Key shaderKey;
91 shaderKey.set(keyMask, keyVal);
92 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080093 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070094 continue;
95 }
Peiyong Linfb530cf2018-12-15 05:07:38 +000096 if (cache.count(shaderKey) == 0) {
97 cache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -070098 shaderCount++;
99 }
100 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700101
102 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700103 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700104 Key shaderKey;
105 shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK |
106 Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK,
107 Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON |
108 Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB);
109 for (int i = 0; i < 4; i++) {
110 shaderKey.set(Key::OPACITY_MASK,
111 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
112 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Peiyong Linfb530cf2018-12-15 05:07:38 +0000113 if (cache.count(shaderKey) == 0) {
114 cache.emplace(shaderKey, generateProgram(shaderKey));
Chia-I Wu93e14df2018-06-04 10:10:17 -0700115 shaderCount++;
116 }
117 }
118 }
119
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700120 nsecs_t timeAfter = systemTime();
121 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
122 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
123}
124
Mathias Agopian3f844832013-08-07 21:24:32 -0700125ProgramCache::Key ProgramCache::computeKey(const Description& description) {
126 Key needs;
127 needs.set(Key::TEXTURE_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700128 !description.textureEnabled
Chia-I Wub027f802017-11-29 14:00:52 -0800129 ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700130 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Chia-I Wub027f802017-11-29 14:00:52 -0800131 ? Key::TEXTURE_EXT
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700132 : description.texture.getTextureTarget() == GL_TEXTURE_2D
Chia-I Wub027f802017-11-29 14:00:52 -0800133 ? Key::TEXTURE_2D
134 : Key::TEXTURE_OFF)
Peiyong Lin46080ef2018-10-26 18:43:14 -0700135 .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800136 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700137 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800138 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700139 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700140 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700141 description.hasInputTransformMatrix()
142 ? Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700143 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Peiyong Lin46080ef2018-10-26 18:43:14 -0700144 description.hasOutputTransformMatrix() || description.hasColorMatrix()
145 ? Key::OUTPUT_TRANSFORM_MATRIX_ON
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700146 : Key::OUTPUT_TRANSFORM_MATRIX_OFF)
147 .set(Key::ROUNDED_CORNERS_MASK,
148 description.cornerRadius > 0
149 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800150
Chia-I Wu131d3762018-01-11 14:35:27 -0800151 needs.set(Key::Y410_BT2020_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700152 description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800153
Peiyong Lina296b0c2018-04-30 16:55:29 -0700154 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700155 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800156 case Description::TransferFunction::LINEAR:
157 default:
158 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
159 break;
160 case Description::TransferFunction::SRGB:
161 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
162 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800163 case Description::TransferFunction::ST2084:
164 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
165 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700166 case Description::TransferFunction::HLG:
167 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
168 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800169 }
170
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700171 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800172 case Description::TransferFunction::LINEAR:
173 default:
174 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
175 break;
176 case Description::TransferFunction::SRGB:
177 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
178 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800179 case Description::TransferFunction::ST2084:
180 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
181 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700182 case Description::TransferFunction::HLG:
183 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
184 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800185 }
186 }
187
Mathias Agopian3f844832013-08-07 21:24:32 -0700188 return needs;
189}
190
Peiyong Lin2542cb02018-04-30 17:29:53 -0700191// Generate EOTF that converts signal values to relative display light,
192// both normalized to [0, 1].
193void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
194 switch (needs.getInputTF()) {
195 case Key::INPUT_TF_SRGB:
196 fs << R"__SHADER__(
197 float EOTF_sRGB(float srgb) {
198 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
199 }
200
201 vec3 EOTF_sRGB(const vec3 srgb) {
202 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
203 }
204
205 vec3 EOTF(const vec3 srgb) {
206 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
207 }
208 )__SHADER__";
209 break;
210 case Key::INPUT_TF_ST2084:
211 fs << R"__SHADER__(
212 vec3 EOTF(const highp vec3 color) {
213 const highp float m1 = (2610.0 / 4096.0) / 4.0;
214 const highp float m2 = (2523.0 / 4096.0) * 128.0;
215 const highp float c1 = (3424.0 / 4096.0);
216 const highp float c2 = (2413.0 / 4096.0) * 32.0;
217 const highp float c3 = (2392.0 / 4096.0) * 32.0;
218
Siddharth Kapoorac1f7c92018-12-05 20:29:06 +0800219 highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700220 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
221 return pow(tmp, 1.0 / vec3(m1));
222 }
223 )__SHADER__";
224 break;
225 case Key::INPUT_TF_HLG:
226 fs << R"__SHADER__(
227 highp float EOTF_channel(const highp float channel) {
228 const highp float a = 0.17883277;
229 const highp float b = 0.28466892;
230 const highp float c = 0.55991073;
231 return channel <= 0.5 ? channel * channel / 3.0 :
232 (exp((channel - c) / a) + b) / 12.0;
233 }
234
235 vec3 EOTF(const highp vec3 color) {
236 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
237 EOTF_channel(color.b));
238 }
239 )__SHADER__";
240 break;
241 default:
242 fs << R"__SHADER__(
243 vec3 EOTF(const vec3 linear) {
244 return linear;
245 }
246 )__SHADER__";
247 break;
248 }
249}
250
Peiyong Lin53f320e2018-04-23 17:31:06 -0700251void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
252 // Convert relative light to absolute light.
253 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700254 case Key::INPUT_TF_ST2084:
255 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700256 highp vec3 ScaleLuminance(highp vec3 color) {
257 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700258 }
259 )__SHADER__";
260 break;
261 case Key::INPUT_TF_HLG:
262 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700263 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700264 // The formula is:
265 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700266 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
267 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700268 }
269 )__SHADER__";
270 break;
271 default:
272 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700273 highp vec3 ScaleLuminance(highp vec3 color) {
274 return color * displayMaxLuminance;
275 }
276 )__SHADER__";
277 break;
278 }
279
280 // Tone map absolute light to display luminance range.
281 switch (needs.getInputTF()) {
282 case Key::INPUT_TF_ST2084:
283 case Key::INPUT_TF_HLG:
284 switch (needs.getOutputTF()) {
285 case Key::OUTPUT_TF_HLG:
286 // Right now when mixed PQ and HLG contents are presented,
287 // HLG content will always be converted to PQ. However, for
288 // completeness, we simply clamp the value to [0.0, 1000.0].
289 fs << R"__SHADER__(
290 highp vec3 ToneMap(highp vec3 color) {
291 return clamp(color, 0.0, 1000.0);
292 }
293 )__SHADER__";
294 break;
295 case Key::OUTPUT_TF_ST2084:
296 fs << R"__SHADER__(
297 highp vec3 ToneMap(highp vec3 color) {
298 return color;
299 }
300 )__SHADER__";
301 break;
302 default:
303 fs << R"__SHADER__(
304 highp vec3 ToneMap(highp vec3 color) {
305 const float maxMasteringLumi = 1000.0;
306 const float maxContentLumi = 1000.0;
307 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
308 float maxOutLumi = displayMaxLuminance;
309
310 float nits = color.y;
311
312 // clamp to max input luminance
313 nits = clamp(nits, 0.0, maxInLumi);
314
315 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
316 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700317 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700318 } else {
319 // three control points
320 const float x0 = 10.0;
321 const float y0 = 17.0;
322 float x1 = maxOutLumi * 0.75;
323 float y1 = x1;
324 float x2 = x1 + (maxInLumi - x1) / 2.0;
325 float y2 = y1 + (maxOutLumi - y1) * 0.75;
326
327 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700328 float h12 = x2 - x1;
329 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700330 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700331 float m1 = (y2 - y1) / h12;
332 float m3 = (maxOutLumi - y2) / h23;
333 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700334
335 if (nits < x0) {
336 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700337 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700338 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700339 } else if (nits < x1) {
340 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700341 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700342 nits = y0 + (nits - x0) * slope;
343 } else if (nits < x2) {
344 // scale [x1, x2] to [y1, y2] using Hermite interp
345 float t = (nits - x1) / h12;
346 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
347 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
348 } else {
349 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
350 float t = (nits - x2) / h23;
351 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
352 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
353 }
354 }
355
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700356 // color.y is greater than x0 and is thus non-zero
357 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700358 }
359 )__SHADER__";
360 break;
361 }
362 break;
363 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700364 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700365 fs << R"__SHADER__(
366 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700367 const float maxOutLumi = 3000.0;
368
369 const float x0 = 5.0;
370 const float y0 = 2.5;
371 float x1 = displayMaxLuminance * 0.7;
372 float y1 = maxOutLumi * 0.15;
373 float x2 = displayMaxLuminance * 0.9;
374 float y2 = maxOutLumi * 0.45;
375 float x3 = displayMaxLuminance;
376 float y3 = maxOutLumi;
377
378 float c1 = y1 / 3.0;
379 float c2 = y2 / 2.0;
380 float c3 = y3 / 1.5;
381
382 float nits = color.y;
383
384 float scale;
385 if (nits <= x0) {
386 // scale [0.0, x0] to [0.0, y0] linearly
387 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700388 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700389 } else if (nits <= x1) {
390 // scale [x0, x1] to [y0, y1] using a curve
391 float t = (nits - x0) / (x1 - x0);
392 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
393 } else if (nits <= x2) {
394 // scale [x1, x2] to [y1, y2] using a curve
395 float t = (nits - x1) / (x2 - x1);
396 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
397 } else {
398 // scale [x2, x3] to [y2, y3] using a curve
399 float t = (nits - x2) / (x3 - x2);
400 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
401 }
402
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700403 // color.y is greater than x0 and is thus non-zero
404 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700405 }
406 )__SHADER__";
407 break;
408 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700409
410 // convert absolute light to relative light.
411 switch (needs.getOutputTF()) {
412 case Key::OUTPUT_TF_ST2084:
413 fs << R"__SHADER__(
414 highp vec3 NormalizeLuminance(highp vec3 color) {
415 return color / 10000.0;
416 }
417 )__SHADER__";
418 break;
419 case Key::OUTPUT_TF_HLG:
420 fs << R"__SHADER__(
421 highp vec3 NormalizeLuminance(highp vec3 color) {
422 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
423 }
424 )__SHADER__";
425 break;
426 default:
427 fs << R"__SHADER__(
428 highp vec3 NormalizeLuminance(highp vec3 color) {
429 return color / displayMaxLuminance;
430 }
431 )__SHADER__";
432 break;
433 }
434}
435
436// Generate OOTF that modifies the relative scence light to relative display light.
437void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
438 if (!needs.needsToneMapping()) {
439 fs << R"__SHADER__(
440 highp vec3 OOTF(const highp vec3 color) {
441 return color;
442 }
443 )__SHADER__";
444 } else {
445 generateToneMappingProcess(fs, needs);
446 fs << R"__SHADER__(
447 highp vec3 OOTF(const highp vec3 color) {
448 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
449 }
450 )__SHADER__";
451 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700452}
453
454// Generate OETF that converts relative display light to signal values,
455// both normalized to [0, 1]
456void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
457 switch (needs.getOutputTF()) {
458 case Key::OUTPUT_TF_SRGB:
459 fs << R"__SHADER__(
460 float OETF_sRGB(const float linear) {
461 return linear <= 0.0031308 ?
462 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
463 }
464
465 vec3 OETF_sRGB(const vec3 linear) {
466 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
467 }
468
469 vec3 OETF(const vec3 linear) {
470 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
471 }
472 )__SHADER__";
473 break;
474 case Key::OUTPUT_TF_ST2084:
475 fs << R"__SHADER__(
476 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700477 const highp float m1 = (2610.0 / 4096.0) / 4.0;
478 const highp float m2 = (2523.0 / 4096.0) * 128.0;
479 const highp float c1 = (3424.0 / 4096.0);
480 const highp float c2 = (2413.0 / 4096.0) * 32.0;
481 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700482
Peiyong Lin834be492018-05-18 15:12:55 -0700483 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700484 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
485 return pow(tmp, vec3(m2));
486 }
487 )__SHADER__";
488 break;
489 case Key::OUTPUT_TF_HLG:
490 fs << R"__SHADER__(
491 highp float OETF_channel(const highp float channel) {
492 const highp float a = 0.17883277;
493 const highp float b = 0.28466892;
494 const highp float c = 0.55991073;
495 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
496 a * log(12.0 * channel - b) + c;
497 }
498
499 vec3 OETF(const highp vec3 color) {
500 return vec3(OETF_channel(color.r), OETF_channel(color.g),
501 OETF_channel(color.b));
502 }
503 )__SHADER__";
504 break;
505 default:
506 fs << R"__SHADER__(
507 vec3 OETF(const vec3 linear) {
508 return linear;
509 }
510 )__SHADER__";
511 break;
512 }
513}
514
Mathias Agopian3f844832013-08-07 21:24:32 -0700515String8 ProgramCache::generateVertexShader(const Key& needs) {
516 Formatter vs;
517 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800518 vs << "attribute vec4 texCoords;"
519 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700520 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700521 if (needs.hasRoundedCorners()) {
522 vs << "attribute lowp vec4 cropCoords;";
523 vs << "varying lowp vec2 outCropCoords;";
524 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700525 vs << "attribute vec4 position;"
526 << "uniform mat4 projection;"
527 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800528 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700529 if (needs.isTexturing()) {
530 vs << "outTexCoords = (texture * texCoords).st;";
531 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700532 if (needs.hasRoundedCorners()) {
533 vs << "outCropCoords = cropCoords.st;";
534 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700535 vs << dedent << "}";
536 return vs.getString();
537}
538
539String8 ProgramCache::generateFragmentShader(const Key& needs) {
540 Formatter fs;
541 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
542 fs << "#extension GL_OES_EGL_image_external : require";
543 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700544
545 // default precision is required-ish in fragment shaders
546 fs << "precision mediump float;";
547
Mathias Agopian3f844832013-08-07 21:24:32 -0700548 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
549 fs << "uniform samplerExternalOES sampler;"
550 << "varying vec2 outTexCoords;";
551 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
552 fs << "uniform sampler2D sampler;"
553 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700554 }
555
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700556 if (needs.hasRoundedCorners()) {
557 // Rounded corners implementation using a signed distance function.
558 fs << R"__SHADER__(
559 uniform float cornerRadius;
560 uniform vec2 cropCenter;
561 varying vec2 outCropCoords;
562
563 /**
564 * This function takes the current crop coordinates and calculates an alpha value based
565 * on the corner radius and distance from the crop center.
566 */
567 float applyCornerRadius(vec2 cropCoords)
568 {
569 vec2 position = cropCoords - cropCenter;
570 vec2 dist = abs(position) + vec2(cornerRadius) - cropCenter;
571 float plane = length(max(dist, vec2(0.0)));
572 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
573 }
574 )__SHADER__";
575 }
576
chaviw13fdc492017-06-27 12:40:18 -0700577 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700578 fs << "uniform vec4 color;";
579 }
chaviw13fdc492017-06-27 12:40:18 -0700580
Chia-I Wu131d3762018-01-11 14:35:27 -0800581 if (needs.isY410BT2020()) {
582 fs << R"__SHADER__(
583 vec3 convertY410BT2020(const vec3 color) {
584 const vec3 offset = vec3(0.0625, 0.5, 0.5);
585 const mat3 transform = mat3(
586 vec3(1.1678, 1.1678, 1.1678),
587 vec3( 0.0, -0.1878, 2.1481),
588 vec3(1.6836, -0.6523, 0.0));
589 // Y is in G, U is in R, and V is in B
590 return clamp(transform * (color.grb - offset), 0.0, 1.0);
591 }
592 )__SHADER__";
593 }
594
Peiyong Lina296b0c2018-04-30 16:55:29 -0700595 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700596 // Currently, display maximum luminance is needed when doing tone mapping.
597 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700598 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700599 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700600
Peiyong Lina296b0c2018-04-30 16:55:29 -0700601 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700602 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700603 fs << R"__SHADER__(
604 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700605 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700606 }
607 )__SHADER__";
608 } else {
609 fs << R"__SHADER__(
610 highp vec3 InputTransform(const highp vec3 color) {
611 return color;
612 }
613 )__SHADER__";
614 }
615
616 // the transformation from a wider colorspace to a narrower one can
617 // result in >1.0 or <0.0 pixel values
618 if (needs.hasOutputTransformMatrix()) {
619 fs << "uniform mat4 outputTransformMatrix;";
620 fs << R"__SHADER__(
621 highp vec3 OutputTransform(const highp vec3 color) {
622 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
623 }
624 )__SHADER__";
625 } else {
626 fs << R"__SHADER__(
627 highp vec3 OutputTransform(const highp vec3 color) {
628 return clamp(color, 0.0, 1.0);
629 }
630 )__SHADER__";
631 }
632
Peiyong Lin2542cb02018-04-30 17:29:53 -0700633 generateEOTF(fs, needs);
634 generateOOTF(fs, needs);
635 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700636 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800637
Mathias Agopian3f844832013-08-07 21:24:32 -0700638 fs << "void main(void) {" << indent;
639 if (needs.isTexturing()) {
640 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800641 if (needs.isY410BT2020()) {
642 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
643 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700644 } else {
chaviw13fdc492017-06-27 12:40:18 -0700645 fs << "gl_FragColor.rgb = color.rgb;";
646 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700647 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700648 if (needs.isOpaque()) {
649 fs << "gl_FragColor.a = 1.0;";
650 }
chaviw13fdc492017-06-27 12:40:18 -0700651 if (needs.hasAlpha()) {
652 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700653 if (needs.isPremultiplied()) {
654 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700655 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700656 } else {
chaviw13fdc492017-06-27 12:40:18 -0700657 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700658 }
659 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700660
Peiyong Lina296b0c2018-04-30 16:55:29 -0700661 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700662 if (!needs.isOpaque() && needs.isPremultiplied()) {
663 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700664 // avoid divide by 0 by adding 0.5/256 to the alpha channel
665 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700666 }
Peiyong Lin46080ef2018-10-26 18:43:14 -0700667 fs << "gl_FragColor.rgb = "
668 "OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700669 if (!needs.isOpaque() && needs.isPremultiplied()) {
670 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700671 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700672 }
673 }
674
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700675 if (needs.hasRoundedCorners()) {
676 if (needs.isPremultiplied()) {
677 fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
678 } else {
679 fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
680 }
681 }
682
Mathias Agopian3f844832013-08-07 21:24:32 -0700683 fs << dedent << "}";
684 return fs.getString();
685}
686
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700687std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700688 ATRACE_CALL();
689
Mathias Agopian3f844832013-08-07 21:24:32 -0700690 // vertex shader
691 String8 vs = generateVertexShader(needs);
692
693 // fragment shader
694 String8 fs = generateFragmentShader(needs);
695
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700696 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700697}
698
Peiyong Linfb530cf2018-12-15 05:07:38 +0000699void ProgramCache::useProgram(EGLContext context, const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700700 // generate the key for the shader based on the description
701 Key needs(computeKey(description));
702
Chia-I Wub027f802017-11-29 14:00:52 -0800703 // look-up the program in the cache
Peiyong Linfb530cf2018-12-15 05:07:38 +0000704 auto& cache = mCaches[context];
705 auto it = cache.find(needs);
706 if (it == cache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700707 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700708 nsecs_t time = systemTime();
Peiyong Linfb530cf2018-12-15 05:07:38 +0000709 it = cache.emplace(needs, generateProgram(needs)).first;
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700710 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700711
Peiyong Linfb530cf2018-12-15 05:07:38 +0000712 ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)",
713 context, needs.mKey, uint32_t(ns2ms(time)), cache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700714 }
715
716 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700717 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700718 if (program->isValid()) {
719 program->use();
720 program->setUniforms(description);
721 }
722}
723
Peiyong Lin46080ef2018-10-26 18:43:14 -0700724} // namespace gl
725} // namespace renderengine
726} // namespace android