blob: 5b61db9fbd01f9e7bcb57cd581a28f73c14717cf [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
17#include <GLES2/gl2.h>
18#include <GLES2/gl2ext.h>
19
20#include <utils/String8.h>
21
Mathias Agopian3f844832013-08-07 21:24:32 -070022#include "Description.h"
Chia-I Wub027f802017-11-29 14:00:52 -080023#include "Program.h"
24#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070025
26namespace android {
27// -----------------------------------------------------------------------------------------------
28
Mathias Agopian3f844832013-08-07 21:24:32 -070029/*
30 * A simple formatter class to automatically add the endl and
31 * manage the indentation.
32 */
33
34class Formatter;
35static Formatter& indent(Formatter& f);
36static Formatter& dedent(Formatter& f);
37
38class Formatter {
39 String8 mString;
40 int mIndent;
41 typedef Formatter& (*FormaterManipFunc)(Formatter&);
42 friend Formatter& indent(Formatter& f);
43 friend Formatter& dedent(Formatter& f);
Chia-I Wub027f802017-11-29 14:00:52 -080044
Mathias Agopian3f844832013-08-07 21:24:32 -070045public:
Andy McFadden892f22d2013-08-15 10:05:01 -070046 Formatter() : mIndent(0) {}
47
Chia-I Wub027f802017-11-29 14:00:52 -080048 String8 getString() const { return mString; }
Mathias Agopian3f844832013-08-07 21:24:32 -070049
Chia-I Wub027f802017-11-29 14:00:52 -080050 friend Formatter& operator<<(Formatter& out, const char* in) {
51 for (int i = 0; i < out.mIndent; i++) {
Mathias Agopian3f844832013-08-07 21:24:32 -070052 out.mString.append(" ");
53 }
54 out.mString.append(in);
55 out.mString.append("\n");
56 return out;
57 }
Chia-I Wub027f802017-11-29 14:00:52 -080058 friend inline Formatter& operator<<(Formatter& out, const String8& in) {
59 return operator<<(out, in.string());
Mathias Agopian3f844832013-08-07 21:24:32 -070060 }
61 friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) {
62 return (*func)(to);
63 }
64};
65Formatter& indent(Formatter& f) {
66 f.mIndent++;
67 return f;
68}
69Formatter& dedent(Formatter& f) {
70 f.mIndent--;
71 return f;
72}
73
74// -----------------------------------------------------------------------------------------------
75
76ANDROID_SINGLETON_STATIC_INSTANCE(ProgramCache)
77
Mathias Agopian3f844832013-08-07 21:24:32 -070078ProgramCache::ProgramCache() {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070079 // Until surfaceflinger has a dependable blob cache on the filesystem,
80 // generate shaders on initialization so as to avoid jank.
81 primeCache();
Mathias Agopian3f844832013-08-07 21:24:32 -070082}
83
Chia-I Wub027f802017-11-29 14:00:52 -080084ProgramCache::~ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070085
Riley Andrewsa51fafc2014-09-29 13:29:40 -070086void ProgramCache::primeCache() {
87 uint32_t shaderCount = 0;
Chia-I Wub027f802017-11-29 14:00:52 -080088 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070089 // Prime the cache for all combinations of the above masks,
90 // leaving off the experimental color matrix mask options.
91
92 nsecs_t timeBefore = systemTime();
93 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
94 Key shaderKey;
95 shaderKey.set(keyMask, keyVal);
96 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080097 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070098 continue;
99 }
100 Program* program = mCache.valueFor(shaderKey);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800101 if (program == nullptr) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700102 program = generateProgram(shaderKey);
103 mCache.add(shaderKey, program);
104 shaderCount++;
105 }
106 }
107 nsecs_t timeAfter = systemTime();
108 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
109 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
110}
111
Mathias Agopian3f844832013-08-07 21:24:32 -0700112ProgramCache::Key ProgramCache::computeKey(const Description& description) {
113 Key needs;
114 needs.set(Key::TEXTURE_MASK,
Chia-I Wub027f802017-11-29 14:00:52 -0800115 !description.mTextureEnabled
116 ? Key::TEXTURE_OFF
117 : description.mTexture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
118 ? Key::TEXTURE_EXT
119 : description.mTexture.getTextureTarget() == GL_TEXTURE_2D
120 ? Key::TEXTURE_2D
121 : Key::TEXTURE_OFF)
122 .set(Key::ALPHA_MASK,
123 (description.mColor.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
124 .set(Key::BLEND_MASK,
125 description.mPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
126 .set(Key::OPACITY_MASK,
127 description.mOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700128 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
129 description.hasInputTransformMatrix() ?
130 Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
131 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700132 description.hasOutputTransformMatrix() || description.hasColorMatrix() ||
133 (!description.hasInputTransformMatrix() && description.hasSaturationMatrix()) ?
Peiyong Lina296b0c2018-04-30 16:55:29 -0700134 Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800135
Chia-I Wu131d3762018-01-11 14:35:27 -0800136 needs.set(Key::Y410_BT2020_MASK,
137 description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
138
Peiyong Lina296b0c2018-04-30 16:55:29 -0700139 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800140 switch (description.mInputTransferFunction) {
141 case Description::TransferFunction::LINEAR:
142 default:
143 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
144 break;
145 case Description::TransferFunction::SRGB:
146 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
147 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800148 case Description::TransferFunction::ST2084:
149 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
150 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700151 case Description::TransferFunction::HLG:
152 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
153 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800154 }
155
156 switch (description.mOutputTransferFunction) {
157 case Description::TransferFunction::LINEAR:
158 default:
159 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
160 break;
161 case Description::TransferFunction::SRGB:
162 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
163 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800164 case Description::TransferFunction::ST2084:
165 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
166 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700167 case Description::TransferFunction::HLG:
168 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
169 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800170 }
171 }
172
Mathias Agopian3f844832013-08-07 21:24:32 -0700173 return needs;
174}
175
Peiyong Lin2542cb02018-04-30 17:29:53 -0700176// Generate EOTF that converts signal values to relative display light,
177// both normalized to [0, 1].
178void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
179 switch (needs.getInputTF()) {
180 case Key::INPUT_TF_SRGB:
181 fs << R"__SHADER__(
182 float EOTF_sRGB(float srgb) {
183 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
184 }
185
186 vec3 EOTF_sRGB(const vec3 srgb) {
187 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
188 }
189
190 vec3 EOTF(const vec3 srgb) {
191 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
192 }
193 )__SHADER__";
194 break;
195 case Key::INPUT_TF_ST2084:
196 fs << R"__SHADER__(
197 vec3 EOTF(const highp vec3 color) {
198 const highp float m1 = (2610.0 / 4096.0) / 4.0;
199 const highp float m2 = (2523.0 / 4096.0) * 128.0;
200 const highp float c1 = (3424.0 / 4096.0);
201 const highp float c2 = (2413.0 / 4096.0) * 32.0;
202 const highp float c3 = (2392.0 / 4096.0) * 32.0;
203
204 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
205 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
206 return pow(tmp, 1.0 / vec3(m1));
207 }
208 )__SHADER__";
209 break;
210 case Key::INPUT_TF_HLG:
211 fs << R"__SHADER__(
212 highp float EOTF_channel(const highp float channel) {
213 const highp float a = 0.17883277;
214 const highp float b = 0.28466892;
215 const highp float c = 0.55991073;
216 return channel <= 0.5 ? channel * channel / 3.0 :
217 (exp((channel - c) / a) + b) / 12.0;
218 }
219
220 vec3 EOTF(const highp vec3 color) {
221 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
222 EOTF_channel(color.b));
223 }
224 )__SHADER__";
225 break;
226 default:
227 fs << R"__SHADER__(
228 vec3 EOTF(const vec3 linear) {
229 return linear;
230 }
231 )__SHADER__";
232 break;
233 }
234}
235
Peiyong Lin53f320e2018-04-23 17:31:06 -0700236void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
237 // Convert relative light to absolute light.
238 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700239 case Key::INPUT_TF_ST2084:
240 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700241 highp vec3 ScaleLuminance(highp vec3 color) {
242 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700243 }
244 )__SHADER__";
245 break;
246 case Key::INPUT_TF_HLG:
247 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700248 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700249 // The formula is:
250 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700251 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
252 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700253 }
254 )__SHADER__";
255 break;
256 default:
257 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700258 highp vec3 ScaleLuminance(highp vec3 color) {
259 return color * displayMaxLuminance;
260 }
261 )__SHADER__";
262 break;
263 }
264
265 // Tone map absolute light to display luminance range.
266 switch (needs.getInputTF()) {
267 case Key::INPUT_TF_ST2084:
268 case Key::INPUT_TF_HLG:
269 switch (needs.getOutputTF()) {
270 case Key::OUTPUT_TF_HLG:
271 // Right now when mixed PQ and HLG contents are presented,
272 // HLG content will always be converted to PQ. However, for
273 // completeness, we simply clamp the value to [0.0, 1000.0].
274 fs << R"__SHADER__(
275 highp vec3 ToneMap(highp vec3 color) {
276 return clamp(color, 0.0, 1000.0);
277 }
278 )__SHADER__";
279 break;
280 case Key::OUTPUT_TF_ST2084:
281 fs << R"__SHADER__(
282 highp vec3 ToneMap(highp vec3 color) {
283 return color;
284 }
285 )__SHADER__";
286 break;
287 default:
288 fs << R"__SHADER__(
289 highp vec3 ToneMap(highp vec3 color) {
290 const float maxMasteringLumi = 1000.0;
291 const float maxContentLumi = 1000.0;
292 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
293 float maxOutLumi = displayMaxLuminance;
294
295 float nits = color.y;
296
297 // clamp to max input luminance
298 nits = clamp(nits, 0.0, maxInLumi);
299
300 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
301 if (maxInLumi <= maxOutLumi) {
302 nits *= maxOutLumi / maxInLumi;
303 } else {
304 // three control points
305 const float x0 = 10.0;
306 const float y0 = 17.0;
307 float x1 = maxOutLumi * 0.75;
308 float y1 = x1;
309 float x2 = x1 + (maxInLumi - x1) / 2.0;
310 float y2 = y1 + (maxOutLumi - y1) * 0.75;
311
312 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700313 float h12 = x2 - x1;
314 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700315 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700316 float m1 = (y2 - y1) / h12;
317 float m3 = (maxOutLumi - y2) / h23;
318 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700319
320 if (nits < x0) {
321 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700322 float slope = y0 / x0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700323 nits *= slope;
324 } else if (nits < x1) {
325 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700326 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700327 nits = y0 + (nits - x0) * slope;
328 } else if (nits < x2) {
329 // scale [x1, x2] to [y1, y2] using Hermite interp
330 float t = (nits - x1) / h12;
331 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
332 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
333 } else {
334 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
335 float t = (nits - x2) / h23;
336 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
337 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
338 }
339 }
340
341 return color * (nits / max(1e-6, color.y));
342 }
343 )__SHADER__";
344 break;
345 }
346 break;
347 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700348 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700349 fs << R"__SHADER__(
350 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700351 const float maxOutLumi = 3000.0;
352
353 const float x0 = 5.0;
354 const float y0 = 2.5;
355 float x1 = displayMaxLuminance * 0.7;
356 float y1 = maxOutLumi * 0.15;
357 float x2 = displayMaxLuminance * 0.9;
358 float y2 = maxOutLumi * 0.45;
359 float x3 = displayMaxLuminance;
360 float y3 = maxOutLumi;
361
362 float c1 = y1 / 3.0;
363 float c2 = y2 / 2.0;
364 float c3 = y3 / 1.5;
365
366 float nits = color.y;
367
368 float scale;
369 if (nits <= x0) {
370 // scale [0.0, x0] to [0.0, y0] linearly
371 const float slope = y0 / x0;
372 nits *= slope;
373 } else if (nits <= x1) {
374 // scale [x0, x1] to [y0, y1] using a curve
375 float t = (nits - x0) / (x1 - x0);
376 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
377 } else if (nits <= x2) {
378 // scale [x1, x2] to [y1, y2] using a curve
379 float t = (nits - x1) / (x2 - x1);
380 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
381 } else {
382 // scale [x2, x3] to [y2, y3] using a curve
383 float t = (nits - x2) / (x3 - x2);
384 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
385 }
386
387 return color * (nits / max(1e-6, color.y));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700388 }
389 )__SHADER__";
390 break;
391 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700392
393 // convert absolute light to relative light.
394 switch (needs.getOutputTF()) {
395 case Key::OUTPUT_TF_ST2084:
396 fs << R"__SHADER__(
397 highp vec3 NormalizeLuminance(highp vec3 color) {
398 return color / 10000.0;
399 }
400 )__SHADER__";
401 break;
402 case Key::OUTPUT_TF_HLG:
403 fs << R"__SHADER__(
404 highp vec3 NormalizeLuminance(highp vec3 color) {
405 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
406 }
407 )__SHADER__";
408 break;
409 default:
410 fs << R"__SHADER__(
411 highp vec3 NormalizeLuminance(highp vec3 color) {
412 return color / displayMaxLuminance;
413 }
414 )__SHADER__";
415 break;
416 }
417}
418
419// Generate OOTF that modifies the relative scence light to relative display light.
420void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
421 if (!needs.needsToneMapping()) {
422 fs << R"__SHADER__(
423 highp vec3 OOTF(const highp vec3 color) {
424 return color;
425 }
426 )__SHADER__";
427 } else {
428 generateToneMappingProcess(fs, needs);
429 fs << R"__SHADER__(
430 highp vec3 OOTF(const highp vec3 color) {
431 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
432 }
433 )__SHADER__";
434 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700435}
436
437// Generate OETF that converts relative display light to signal values,
438// both normalized to [0, 1]
439void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
440 switch (needs.getOutputTF()) {
441 case Key::OUTPUT_TF_SRGB:
442 fs << R"__SHADER__(
443 float OETF_sRGB(const float linear) {
444 return linear <= 0.0031308 ?
445 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
446 }
447
448 vec3 OETF_sRGB(const vec3 linear) {
449 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
450 }
451
452 vec3 OETF(const vec3 linear) {
453 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
454 }
455 )__SHADER__";
456 break;
457 case Key::OUTPUT_TF_ST2084:
458 fs << R"__SHADER__(
459 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700460 const highp float m1 = (2610.0 / 4096.0) / 4.0;
461 const highp float m2 = (2523.0 / 4096.0) * 128.0;
462 const highp float c1 = (3424.0 / 4096.0);
463 const highp float c2 = (2413.0 / 4096.0) * 32.0;
464 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700465
Peiyong Lin834be492018-05-18 15:12:55 -0700466 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700467 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
468 return pow(tmp, vec3(m2));
469 }
470 )__SHADER__";
471 break;
472 case Key::OUTPUT_TF_HLG:
473 fs << R"__SHADER__(
474 highp float OETF_channel(const highp float channel) {
475 const highp float a = 0.17883277;
476 const highp float b = 0.28466892;
477 const highp float c = 0.55991073;
478 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
479 a * log(12.0 * channel - b) + c;
480 }
481
482 vec3 OETF(const highp vec3 color) {
483 return vec3(OETF_channel(color.r), OETF_channel(color.g),
484 OETF_channel(color.b));
485 }
486 )__SHADER__";
487 break;
488 default:
489 fs << R"__SHADER__(
490 vec3 OETF(const vec3 linear) {
491 return linear;
492 }
493 )__SHADER__";
494 break;
495 }
496}
497
Mathias Agopian3f844832013-08-07 21:24:32 -0700498String8 ProgramCache::generateVertexShader(const Key& needs) {
499 Formatter vs;
500 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800501 vs << "attribute vec4 texCoords;"
502 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700503 }
504 vs << "attribute vec4 position;"
505 << "uniform mat4 projection;"
506 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800507 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700508 if (needs.isTexturing()) {
509 vs << "outTexCoords = (texture * texCoords).st;";
510 }
511 vs << dedent << "}";
512 return vs.getString();
513}
514
515String8 ProgramCache::generateFragmentShader(const Key& needs) {
516 Formatter fs;
517 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
518 fs << "#extension GL_OES_EGL_image_external : require";
519 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700520
521 // default precision is required-ish in fragment shaders
522 fs << "precision mediump float;";
523
Mathias Agopian3f844832013-08-07 21:24:32 -0700524 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
525 fs << "uniform samplerExternalOES sampler;"
526 << "varying vec2 outTexCoords;";
527 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
528 fs << "uniform sampler2D sampler;"
529 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700530 }
531
532 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700533 fs << "uniform vec4 color;";
534 }
chaviw13fdc492017-06-27 12:40:18 -0700535
Chia-I Wu131d3762018-01-11 14:35:27 -0800536 if (needs.isY410BT2020()) {
537 fs << R"__SHADER__(
538 vec3 convertY410BT2020(const vec3 color) {
539 const vec3 offset = vec3(0.0625, 0.5, 0.5);
540 const mat3 transform = mat3(
541 vec3(1.1678, 1.1678, 1.1678),
542 vec3( 0.0, -0.1878, 2.1481),
543 vec3(1.6836, -0.6523, 0.0));
544 // Y is in G, U is in R, and V is in B
545 return clamp(transform * (color.grb - offset), 0.0, 1.0);
546 }
547 )__SHADER__";
548 }
549
Peiyong Lina296b0c2018-04-30 16:55:29 -0700550 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700551 // Currently, display maximum luminance is needed when doing tone mapping.
552 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700553 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700554 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700555
Peiyong Lina296b0c2018-04-30 16:55:29 -0700556 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700557 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700558 fs << R"__SHADER__(
559 highp vec3 InputTransform(const highp vec3 color) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700560 return vec3(inputTransformMatrix * vec4(color, 1.0));
Peiyong Lina296b0c2018-04-30 16:55:29 -0700561 }
562 )__SHADER__";
563 } else {
564 fs << R"__SHADER__(
565 highp vec3 InputTransform(const highp vec3 color) {
566 return color;
567 }
568 )__SHADER__";
569 }
570
571 // the transformation from a wider colorspace to a narrower one can
572 // result in >1.0 or <0.0 pixel values
573 if (needs.hasOutputTransformMatrix()) {
574 fs << "uniform mat4 outputTransformMatrix;";
575 fs << R"__SHADER__(
576 highp vec3 OutputTransform(const highp vec3 color) {
577 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
578 }
579 )__SHADER__";
580 } else {
581 fs << R"__SHADER__(
582 highp vec3 OutputTransform(const highp vec3 color) {
583 return clamp(color, 0.0, 1.0);
584 }
585 )__SHADER__";
586 }
587
Peiyong Lin2542cb02018-04-30 17:29:53 -0700588 generateEOTF(fs, needs);
589 generateOOTF(fs, needs);
590 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700591 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800592
Mathias Agopian3f844832013-08-07 21:24:32 -0700593 fs << "void main(void) {" << indent;
594 if (needs.isTexturing()) {
595 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800596 if (needs.isY410BT2020()) {
597 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
598 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700599 } else {
chaviw13fdc492017-06-27 12:40:18 -0700600 fs << "gl_FragColor.rgb = color.rgb;";
601 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700602 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700603 if (needs.isOpaque()) {
604 fs << "gl_FragColor.a = 1.0;";
605 }
chaviw13fdc492017-06-27 12:40:18 -0700606 if (needs.hasAlpha()) {
607 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700608 if (needs.isPremultiplied()) {
609 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700610 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700611 } else {
chaviw13fdc492017-06-27 12:40:18 -0700612 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700613 }
614 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700615
Peiyong Lina296b0c2018-04-30 16:55:29 -0700616 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700617 if (!needs.isOpaque() && needs.isPremultiplied()) {
618 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700619 // avoid divide by 0 by adding 0.5/256 to the alpha channel
620 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700621 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700622 fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700623 if (!needs.isOpaque() && needs.isPremultiplied()) {
624 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700625 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700626 }
627 }
628
Mathias Agopian3f844832013-08-07 21:24:32 -0700629 fs << dedent << "}";
630 return fs.getString();
631}
632
633Program* ProgramCache::generateProgram(const Key& needs) {
634 // vertex shader
635 String8 vs = generateVertexShader(needs);
636
637 // fragment shader
638 String8 fs = generateFragmentShader(needs);
639
640 Program* program = new Program(needs, vs.string(), fs.string());
641 return program;
642}
643
644void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700645 // generate the key for the shader based on the description
646 Key needs(computeKey(description));
647
Chia-I Wub027f802017-11-29 14:00:52 -0800648 // look-up the program in the cache
Mathias Agopian3f844832013-08-07 21:24:32 -0700649 Program* program = mCache.valueFor(needs);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800650 if (program == nullptr) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700651 // we didn't find our program, so generate one...
652 nsecs_t time = -systemTime();
653 program = generateProgram(needs);
654 mCache.add(needs, program);
655 time += systemTime();
656
Chia-I Wub027f802017-11-29 14:00:52 -0800657 // ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)",
Mathias Agopian3f844832013-08-07 21:24:32 -0700658 // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size());
659 }
660
661 // here we have a suitable program for this description
662 if (program->isValid()) {
663 program->use();
664 program->setUniforms(description);
665 }
666}
667
Mathias Agopian3f844832013-08-07 21:24:32 -0700668} /* namespace android */