blob: b7101e05b757f93cb9e3ba9ca976a6c9d9ef68e2 [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 Lincbc184f2018-08-22 13:24:10 -070019#include <renderengine/ProgramCache.h>
20
Mathias Agopian3f844832013-08-07 21:24:32 -070021#include <GLES2/gl2.h>
22#include <GLES2/gl2ext.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070023#include <renderengine/Description.h>
24#include <renderengine/Program.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>
Mathias Agopian3f844832013-08-07 21:24:32 -070027
Mathias Agopian3f844832013-08-07 21:24:32 -070028namespace android {
29// -----------------------------------------------------------------------------------------------
30
Mathias Agopian3f844832013-08-07 21:24:32 -070031/*
32 * A simple formatter class to automatically add the endl and
33 * manage the indentation.
34 */
35
36class Formatter;
37static Formatter& indent(Formatter& f);
38static Formatter& dedent(Formatter& f);
39
40class Formatter {
41 String8 mString;
42 int mIndent;
43 typedef Formatter& (*FormaterManipFunc)(Formatter&);
44 friend Formatter& indent(Formatter& f);
45 friend Formatter& dedent(Formatter& f);
Chia-I Wub027f802017-11-29 14:00:52 -080046
Mathias Agopian3f844832013-08-07 21:24:32 -070047public:
Andy McFadden892f22d2013-08-15 10:05:01 -070048 Formatter() : mIndent(0) {}
49
Chia-I Wub027f802017-11-29 14:00:52 -080050 String8 getString() const { return mString; }
Mathias Agopian3f844832013-08-07 21:24:32 -070051
Chia-I Wub027f802017-11-29 14:00:52 -080052 friend Formatter& operator<<(Formatter& out, const char* in) {
53 for (int i = 0; i < out.mIndent; i++) {
Mathias Agopian3f844832013-08-07 21:24:32 -070054 out.mString.append(" ");
55 }
56 out.mString.append(in);
57 out.mString.append("\n");
58 return out;
59 }
Chia-I Wub027f802017-11-29 14:00:52 -080060 friend inline Formatter& operator<<(Formatter& out, const String8& in) {
61 return operator<<(out, in.string());
Mathias Agopian3f844832013-08-07 21:24:32 -070062 }
63 friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) {
64 return (*func)(to);
65 }
66};
67Formatter& indent(Formatter& f) {
68 f.mIndent++;
69 return f;
70}
71Formatter& dedent(Formatter& f) {
72 f.mIndent--;
73 return f;
74}
75
76// -----------------------------------------------------------------------------------------------
77
78ANDROID_SINGLETON_STATIC_INSTANCE(ProgramCache)
79
Chia-I Wu93e14df2018-06-04 10:10:17 -070080ProgramCache::ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070081
Chia-I Wub027f802017-11-29 14:00:52 -080082ProgramCache::~ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070083
Peiyong Lin13effd12018-07-24 17:01:47 -070084void ProgramCache::primeCache(bool useColorManagement) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070085 uint32_t shaderCount = 0;
Chia-I Wub027f802017-11-29 14:00:52 -080086 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070087 // Prime the cache for all combinations of the above masks,
88 // leaving off the experimental color matrix mask options.
89
90 nsecs_t timeBefore = systemTime();
91 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
92 Key shaderKey;
93 shaderKey.set(keyMask, keyVal);
94 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080095 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070096 continue;
97 }
98 Program* program = mCache.valueFor(shaderKey);
Peiyong Lin566a3b42018-01-09 18:22:43 -080099 if (program == nullptr) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700100 program = generateProgram(shaderKey);
101 mCache.add(shaderKey, program);
102 shaderCount++;
103 }
104 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700105
106 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700107 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700108 Key shaderKey;
109 shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK |
110 Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK,
111 Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON |
112 Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB);
113 for (int i = 0; i < 4; i++) {
114 shaderKey.set(Key::OPACITY_MASK,
115 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
116 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
117 Program* program = mCache.valueFor(shaderKey);
118 if (program == nullptr) {
119 program = generateProgram(shaderKey);
120 mCache.add(shaderKey, program);
121 shaderCount++;
122 }
123 }
124 }
125
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700126 nsecs_t timeAfter = systemTime();
127 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
128 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
129}
130
Mathias Agopian3f844832013-08-07 21:24:32 -0700131ProgramCache::Key ProgramCache::computeKey(const Description& description) {
132 Key needs;
133 needs.set(Key::TEXTURE_MASK,
Chia-I Wub027f802017-11-29 14:00:52 -0800134 !description.mTextureEnabled
135 ? Key::TEXTURE_OFF
136 : description.mTexture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
137 ? Key::TEXTURE_EXT
138 : description.mTexture.getTextureTarget() == GL_TEXTURE_2D
139 ? Key::TEXTURE_2D
140 : Key::TEXTURE_OFF)
141 .set(Key::ALPHA_MASK,
142 (description.mColor.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
143 .set(Key::BLEND_MASK,
144 description.mPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
145 .set(Key::OPACITY_MASK,
146 description.mOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700147 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
148 description.hasInputTransformMatrix() ?
149 Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
150 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Chia-I Wud49d6692018-06-27 07:17:41 +0800151 description.hasOutputTransformMatrix() || description.hasColorMatrix() ?
Peiyong Lina296b0c2018-04-30 16:55:29 -0700152 Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800153
Chia-I Wu131d3762018-01-11 14:35:27 -0800154 needs.set(Key::Y410_BT2020_MASK,
155 description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
156
Peiyong Lina296b0c2018-04-30 16:55:29 -0700157 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800158 switch (description.mInputTransferFunction) {
159 case Description::TransferFunction::LINEAR:
160 default:
161 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
162 break;
163 case Description::TransferFunction::SRGB:
164 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
165 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800166 case Description::TransferFunction::ST2084:
167 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
168 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700169 case Description::TransferFunction::HLG:
170 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
171 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800172 }
173
174 switch (description.mOutputTransferFunction) {
175 case Description::TransferFunction::LINEAR:
176 default:
177 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
178 break;
179 case Description::TransferFunction::SRGB:
180 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
181 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800182 case Description::TransferFunction::ST2084:
183 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
184 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700185 case Description::TransferFunction::HLG:
186 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
187 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800188 }
189 }
190
Mathias Agopian3f844832013-08-07 21:24:32 -0700191 return needs;
192}
193
Peiyong Lin2542cb02018-04-30 17:29:53 -0700194// Generate EOTF that converts signal values to relative display light,
195// both normalized to [0, 1].
196void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
197 switch (needs.getInputTF()) {
198 case Key::INPUT_TF_SRGB:
199 fs << R"__SHADER__(
200 float EOTF_sRGB(float srgb) {
201 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
202 }
203
204 vec3 EOTF_sRGB(const vec3 srgb) {
205 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
206 }
207
208 vec3 EOTF(const vec3 srgb) {
209 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
210 }
211 )__SHADER__";
212 break;
213 case Key::INPUT_TF_ST2084:
214 fs << R"__SHADER__(
215 vec3 EOTF(const highp vec3 color) {
216 const highp float m1 = (2610.0 / 4096.0) / 4.0;
217 const highp float m2 = (2523.0 / 4096.0) * 128.0;
218 const highp float c1 = (3424.0 / 4096.0);
219 const highp float c2 = (2413.0 / 4096.0) * 32.0;
220 const highp float c3 = (2392.0 / 4096.0) * 32.0;
221
222 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
223 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
224 return pow(tmp, 1.0 / vec3(m1));
225 }
226 )__SHADER__";
227 break;
228 case Key::INPUT_TF_HLG:
229 fs << R"__SHADER__(
230 highp float EOTF_channel(const highp float channel) {
231 const highp float a = 0.17883277;
232 const highp float b = 0.28466892;
233 const highp float c = 0.55991073;
234 return channel <= 0.5 ? channel * channel / 3.0 :
235 (exp((channel - c) / a) + b) / 12.0;
236 }
237
238 vec3 EOTF(const highp vec3 color) {
239 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
240 EOTF_channel(color.b));
241 }
242 )__SHADER__";
243 break;
244 default:
245 fs << R"__SHADER__(
246 vec3 EOTF(const vec3 linear) {
247 return linear;
248 }
249 )__SHADER__";
250 break;
251 }
252}
253
Peiyong Lin53f320e2018-04-23 17:31:06 -0700254void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
255 // Convert relative light to absolute light.
256 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700257 case Key::INPUT_TF_ST2084:
258 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700259 highp vec3 ScaleLuminance(highp vec3 color) {
260 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700261 }
262 )__SHADER__";
263 break;
264 case Key::INPUT_TF_HLG:
265 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700266 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700267 // The formula is:
268 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700269 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
270 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700271 }
272 )__SHADER__";
273 break;
274 default:
275 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700276 highp vec3 ScaleLuminance(highp vec3 color) {
277 return color * displayMaxLuminance;
278 }
279 )__SHADER__";
280 break;
281 }
282
283 // Tone map absolute light to display luminance range.
284 switch (needs.getInputTF()) {
285 case Key::INPUT_TF_ST2084:
286 case Key::INPUT_TF_HLG:
287 switch (needs.getOutputTF()) {
288 case Key::OUTPUT_TF_HLG:
289 // Right now when mixed PQ and HLG contents are presented,
290 // HLG content will always be converted to PQ. However, for
291 // completeness, we simply clamp the value to [0.0, 1000.0].
292 fs << R"__SHADER__(
293 highp vec3 ToneMap(highp vec3 color) {
294 return clamp(color, 0.0, 1000.0);
295 }
296 )__SHADER__";
297 break;
298 case Key::OUTPUT_TF_ST2084:
299 fs << R"__SHADER__(
300 highp vec3 ToneMap(highp vec3 color) {
301 return color;
302 }
303 )__SHADER__";
304 break;
305 default:
306 fs << R"__SHADER__(
307 highp vec3 ToneMap(highp vec3 color) {
308 const float maxMasteringLumi = 1000.0;
309 const float maxContentLumi = 1000.0;
310 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
311 float maxOutLumi = displayMaxLuminance;
312
313 float nits = color.y;
314
315 // clamp to max input luminance
316 nits = clamp(nits, 0.0, maxInLumi);
317
318 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
319 if (maxInLumi <= maxOutLumi) {
320 nits *= maxOutLumi / maxInLumi;
321 } else {
322 // three control points
323 const float x0 = 10.0;
324 const float y0 = 17.0;
325 float x1 = maxOutLumi * 0.75;
326 float y1 = x1;
327 float x2 = x1 + (maxInLumi - x1) / 2.0;
328 float y2 = y1 + (maxOutLumi - y1) * 0.75;
329
330 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700331 float h12 = x2 - x1;
332 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700333 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700334 float m1 = (y2 - y1) / h12;
335 float m3 = (maxOutLumi - y2) / h23;
336 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700337
338 if (nits < x0) {
339 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700340 float slope = y0 / x0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700341 nits *= slope;
342 } else if (nits < x1) {
343 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700344 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700345 nits = y0 + (nits - x0) * slope;
346 } else if (nits < x2) {
347 // scale [x1, x2] to [y1, y2] using Hermite interp
348 float t = (nits - x1) / h12;
349 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
350 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
351 } else {
352 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
353 float t = (nits - x2) / h23;
354 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
355 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
356 }
357 }
358
359 return color * (nits / max(1e-6, color.y));
360 }
361 )__SHADER__";
362 break;
363 }
364 break;
365 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700366 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700367 fs << R"__SHADER__(
368 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700369 const float maxOutLumi = 3000.0;
370
371 const float x0 = 5.0;
372 const float y0 = 2.5;
373 float x1 = displayMaxLuminance * 0.7;
374 float y1 = maxOutLumi * 0.15;
375 float x2 = displayMaxLuminance * 0.9;
376 float y2 = maxOutLumi * 0.45;
377 float x3 = displayMaxLuminance;
378 float y3 = maxOutLumi;
379
380 float c1 = y1 / 3.0;
381 float c2 = y2 / 2.0;
382 float c3 = y3 / 1.5;
383
384 float nits = color.y;
385
386 float scale;
387 if (nits <= x0) {
388 // scale [0.0, x0] to [0.0, y0] linearly
389 const float slope = y0 / x0;
390 nits *= slope;
391 } else if (nits <= x1) {
392 // scale [x0, x1] to [y0, y1] using a curve
393 float t = (nits - x0) / (x1 - x0);
394 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
395 } else if (nits <= x2) {
396 // scale [x1, x2] to [y1, y2] using a curve
397 float t = (nits - x1) / (x2 - x1);
398 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
399 } else {
400 // scale [x2, x3] to [y2, y3] using a curve
401 float t = (nits - x2) / (x3 - x2);
402 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
403 }
404
405 return color * (nits / max(1e-6, color.y));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700406 }
407 )__SHADER__";
408 break;
409 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700410
411 // convert absolute light to relative light.
412 switch (needs.getOutputTF()) {
413 case Key::OUTPUT_TF_ST2084:
414 fs << R"__SHADER__(
415 highp vec3 NormalizeLuminance(highp vec3 color) {
416 return color / 10000.0;
417 }
418 )__SHADER__";
419 break;
420 case Key::OUTPUT_TF_HLG:
421 fs << R"__SHADER__(
422 highp vec3 NormalizeLuminance(highp vec3 color) {
423 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
424 }
425 )__SHADER__";
426 break;
427 default:
428 fs << R"__SHADER__(
429 highp vec3 NormalizeLuminance(highp vec3 color) {
430 return color / displayMaxLuminance;
431 }
432 )__SHADER__";
433 break;
434 }
435}
436
437// Generate OOTF that modifies the relative scence light to relative display light.
438void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
439 if (!needs.needsToneMapping()) {
440 fs << R"__SHADER__(
441 highp vec3 OOTF(const highp vec3 color) {
442 return color;
443 }
444 )__SHADER__";
445 } else {
446 generateToneMappingProcess(fs, needs);
447 fs << R"__SHADER__(
448 highp vec3 OOTF(const highp vec3 color) {
449 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
450 }
451 )__SHADER__";
452 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700453}
454
455// Generate OETF that converts relative display light to signal values,
456// both normalized to [0, 1]
457void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
458 switch (needs.getOutputTF()) {
459 case Key::OUTPUT_TF_SRGB:
460 fs << R"__SHADER__(
461 float OETF_sRGB(const float linear) {
462 return linear <= 0.0031308 ?
463 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
464 }
465
466 vec3 OETF_sRGB(const vec3 linear) {
467 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
468 }
469
470 vec3 OETF(const vec3 linear) {
471 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
472 }
473 )__SHADER__";
474 break;
475 case Key::OUTPUT_TF_ST2084:
476 fs << R"__SHADER__(
477 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700478 const highp float m1 = (2610.0 / 4096.0) / 4.0;
479 const highp float m2 = (2523.0 / 4096.0) * 128.0;
480 const highp float c1 = (3424.0 / 4096.0);
481 const highp float c2 = (2413.0 / 4096.0) * 32.0;
482 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700483
Peiyong Lin834be492018-05-18 15:12:55 -0700484 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700485 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
486 return pow(tmp, vec3(m2));
487 }
488 )__SHADER__";
489 break;
490 case Key::OUTPUT_TF_HLG:
491 fs << R"__SHADER__(
492 highp float OETF_channel(const highp float channel) {
493 const highp float a = 0.17883277;
494 const highp float b = 0.28466892;
495 const highp float c = 0.55991073;
496 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
497 a * log(12.0 * channel - b) + c;
498 }
499
500 vec3 OETF(const highp vec3 color) {
501 return vec3(OETF_channel(color.r), OETF_channel(color.g),
502 OETF_channel(color.b));
503 }
504 )__SHADER__";
505 break;
506 default:
507 fs << R"__SHADER__(
508 vec3 OETF(const vec3 linear) {
509 return linear;
510 }
511 )__SHADER__";
512 break;
513 }
514}
515
Mathias Agopian3f844832013-08-07 21:24:32 -0700516String8 ProgramCache::generateVertexShader(const Key& needs) {
517 Formatter vs;
518 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800519 vs << "attribute vec4 texCoords;"
520 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700521 }
522 vs << "attribute vec4 position;"
523 << "uniform mat4 projection;"
524 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800525 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700526 if (needs.isTexturing()) {
527 vs << "outTexCoords = (texture * texCoords).st;";
528 }
529 vs << dedent << "}";
530 return vs.getString();
531}
532
533String8 ProgramCache::generateFragmentShader(const Key& needs) {
534 Formatter fs;
535 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
536 fs << "#extension GL_OES_EGL_image_external : require";
537 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700538
539 // default precision is required-ish in fragment shaders
540 fs << "precision mediump float;";
541
Mathias Agopian3f844832013-08-07 21:24:32 -0700542 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
543 fs << "uniform samplerExternalOES sampler;"
544 << "varying vec2 outTexCoords;";
545 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
546 fs << "uniform sampler2D sampler;"
547 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700548 }
549
550 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700551 fs << "uniform vec4 color;";
552 }
chaviw13fdc492017-06-27 12:40:18 -0700553
Chia-I Wu131d3762018-01-11 14:35:27 -0800554 if (needs.isY410BT2020()) {
555 fs << R"__SHADER__(
556 vec3 convertY410BT2020(const vec3 color) {
557 const vec3 offset = vec3(0.0625, 0.5, 0.5);
558 const mat3 transform = mat3(
559 vec3(1.1678, 1.1678, 1.1678),
560 vec3( 0.0, -0.1878, 2.1481),
561 vec3(1.6836, -0.6523, 0.0));
562 // Y is in G, U is in R, and V is in B
563 return clamp(transform * (color.grb - offset), 0.0, 1.0);
564 }
565 )__SHADER__";
566 }
567
Peiyong Lina296b0c2018-04-30 16:55:29 -0700568 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700569 // Currently, display maximum luminance is needed when doing tone mapping.
570 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700571 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700572 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700573
Peiyong Lina296b0c2018-04-30 16:55:29 -0700574 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700575 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700576 fs << R"__SHADER__(
577 highp vec3 InputTransform(const highp vec3 color) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700578 return vec3(inputTransformMatrix * vec4(color, 1.0));
Peiyong Lina296b0c2018-04-30 16:55:29 -0700579 }
580 )__SHADER__";
581 } else {
582 fs << R"__SHADER__(
583 highp vec3 InputTransform(const highp vec3 color) {
584 return color;
585 }
586 )__SHADER__";
587 }
588
589 // the transformation from a wider colorspace to a narrower one can
590 // result in >1.0 or <0.0 pixel values
591 if (needs.hasOutputTransformMatrix()) {
592 fs << "uniform mat4 outputTransformMatrix;";
593 fs << R"__SHADER__(
594 highp vec3 OutputTransform(const highp vec3 color) {
595 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
596 }
597 )__SHADER__";
598 } else {
599 fs << R"__SHADER__(
600 highp vec3 OutputTransform(const highp vec3 color) {
601 return clamp(color, 0.0, 1.0);
602 }
603 )__SHADER__";
604 }
605
Peiyong Lin2542cb02018-04-30 17:29:53 -0700606 generateEOTF(fs, needs);
607 generateOOTF(fs, needs);
608 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700609 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800610
Mathias Agopian3f844832013-08-07 21:24:32 -0700611 fs << "void main(void) {" << indent;
612 if (needs.isTexturing()) {
613 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800614 if (needs.isY410BT2020()) {
615 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
616 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700617 } else {
chaviw13fdc492017-06-27 12:40:18 -0700618 fs << "gl_FragColor.rgb = color.rgb;";
619 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700620 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700621 if (needs.isOpaque()) {
622 fs << "gl_FragColor.a = 1.0;";
623 }
chaviw13fdc492017-06-27 12:40:18 -0700624 if (needs.hasAlpha()) {
625 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700626 if (needs.isPremultiplied()) {
627 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700628 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700629 } else {
chaviw13fdc492017-06-27 12:40:18 -0700630 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700631 }
632 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700633
Peiyong Lina296b0c2018-04-30 16:55:29 -0700634 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700635 if (!needs.isOpaque() && needs.isPremultiplied()) {
636 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700637 // avoid divide by 0 by adding 0.5/256 to the alpha channel
638 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700639 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700640 fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700641 if (!needs.isOpaque() && needs.isPremultiplied()) {
642 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700643 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700644 }
645 }
646
Mathias Agopian3f844832013-08-07 21:24:32 -0700647 fs << dedent << "}";
648 return fs.getString();
649}
650
651Program* ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700652 ATRACE_CALL();
653
Mathias Agopian3f844832013-08-07 21:24:32 -0700654 // vertex shader
655 String8 vs = generateVertexShader(needs);
656
657 // fragment shader
658 String8 fs = generateFragmentShader(needs);
659
660 Program* program = new Program(needs, vs.string(), fs.string());
661 return program;
662}
663
664void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700665 // generate the key for the shader based on the description
666 Key needs(computeKey(description));
667
Chia-I Wub027f802017-11-29 14:00:52 -0800668 // look-up the program in the cache
Mathias Agopian3f844832013-08-07 21:24:32 -0700669 Program* program = mCache.valueFor(needs);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800670 if (program == nullptr) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700671 // we didn't find our program, so generate one...
672 nsecs_t time = -systemTime();
673 program = generateProgram(needs);
674 mCache.add(needs, program);
675 time += systemTime();
676
Chia-I Wu93e14df2018-06-04 10:10:17 -0700677 ALOGV(">>> generated new program: needs=%08X, time=%u ms (%zu programs)", needs.mKey,
678 uint32_t(ns2ms(time)), mCache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700679 }
680
681 // here we have a suitable program for this description
682 if (program->isValid()) {
683 program->use();
684 program->setUniforms(description);
685 }
686}
687
Mathias Agopian3f844832013-08-07 21:24:32 -0700688} /* namespace android */