blob: 57a772b047eb6da07d8ae6431bc1661a25eff765 [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,
132 description.hasOutputTransformMatrix() || description.hasColorMatrix() ?
133 Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800134
Chia-I Wu131d3762018-01-11 14:35:27 -0800135 needs.set(Key::Y410_BT2020_MASK,
136 description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
137
Peiyong Lina296b0c2018-04-30 16:55:29 -0700138 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800139 switch (description.mInputTransferFunction) {
140 case Description::TransferFunction::LINEAR:
141 default:
142 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
143 break;
144 case Description::TransferFunction::SRGB:
145 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
146 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800147 case Description::TransferFunction::ST2084:
148 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
149 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700150 case Description::TransferFunction::HLG:
151 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
152 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800153 }
154
155 switch (description.mOutputTransferFunction) {
156 case Description::TransferFunction::LINEAR:
157 default:
158 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
159 break;
160 case Description::TransferFunction::SRGB:
161 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
162 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800163 case Description::TransferFunction::ST2084:
164 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
165 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700166 case Description::TransferFunction::HLG:
167 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
168 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800169 }
170 }
171
Mathias Agopian3f844832013-08-07 21:24:32 -0700172 return needs;
173}
174
Peiyong Lin2542cb02018-04-30 17:29:53 -0700175// Generate EOTF that converts signal values to relative display light,
176// both normalized to [0, 1].
177void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
178 switch (needs.getInputTF()) {
179 case Key::INPUT_TF_SRGB:
180 fs << R"__SHADER__(
181 float EOTF_sRGB(float srgb) {
182 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
183 }
184
185 vec3 EOTF_sRGB(const vec3 srgb) {
186 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
187 }
188
189 vec3 EOTF(const vec3 srgb) {
190 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
191 }
192 )__SHADER__";
193 break;
194 case Key::INPUT_TF_ST2084:
195 fs << R"__SHADER__(
196 vec3 EOTF(const highp vec3 color) {
197 const highp float m1 = (2610.0 / 4096.0) / 4.0;
198 const highp float m2 = (2523.0 / 4096.0) * 128.0;
199 const highp float c1 = (3424.0 / 4096.0);
200 const highp float c2 = (2413.0 / 4096.0) * 32.0;
201 const highp float c3 = (2392.0 / 4096.0) * 32.0;
202
203 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
204 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
205 return pow(tmp, 1.0 / vec3(m1));
206 }
207 )__SHADER__";
208 break;
209 case Key::INPUT_TF_HLG:
210 fs << R"__SHADER__(
211 highp float EOTF_channel(const highp float channel) {
212 const highp float a = 0.17883277;
213 const highp float b = 0.28466892;
214 const highp float c = 0.55991073;
215 return channel <= 0.5 ? channel * channel / 3.0 :
216 (exp((channel - c) / a) + b) / 12.0;
217 }
218
219 vec3 EOTF(const highp vec3 color) {
220 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
221 EOTF_channel(color.b));
222 }
223 )__SHADER__";
224 break;
225 default:
226 fs << R"__SHADER__(
227 vec3 EOTF(const vec3 linear) {
228 return linear;
229 }
230 )__SHADER__";
231 break;
232 }
233}
234
235// Generate OOTF that modifies the relative scence light to relative display light.
Peiyong Lin0589adc2018-04-23 17:31:06 -0700236void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
237 // When HDR and non-HDR contents are mixed, or different types of HDR contents are
238 // mixed, we will do a transcoding process to transcode the input content to output
239 // content. Currently, the following conversions handled, they are:
240 // * SDR -> HLG
241 // * SDR -> PQ
242 // * HLG -> PQ
Peiyong Lin2542cb02018-04-30 17:29:53 -0700243
Peiyong Lin0589adc2018-04-23 17:31:06 -0700244 // Convert relative light to absolute light.
245 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700246 case Key::INPUT_TF_ST2084:
247 fs << R"__SHADER__(
Peiyong Lin0589adc2018-04-23 17:31:06 -0700248 highp vec3 ScaleLuminance(color) {
249 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700250 }
251 )__SHADER__";
252 break;
253 case Key::INPUT_TF_HLG:
254 fs << R"__SHADER__(
Peiyong Lin0589adc2018-04-23 17:31:06 -0700255 highp vec3 ScaleLuminance(color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700256 // The formula is:
257 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin0589adc2018-04-23 17:31:06 -0700258 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
259 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700260 }
261 )__SHADER__";
262 break;
263 default:
264 fs << R"__SHADER__(
Peiyong Lin0589adc2018-04-23 17:31:06 -0700265 highp vec3 ScaleLuminance(color) {
266 return color * displayMaxLuminance;
267 }
268 )__SHADER__";
269 break;
270 }
271
272 // Tone map absolute light to display luminance range.
273 switch (needs.getInputTF()) {
274 case Key::INPUT_TF_ST2084:
275 case Key::INPUT_TF_HLG:
276 switch (needs.getOutputTF()) {
277 case Key::OUTPUT_TF_HLG:
278 // Right now when mixed PQ and HLG contents are presented,
279 // HLG content will always be converted to PQ. However, for
280 // completeness, we simply clamp the value to [0.0, 1000.0].
281 fs << R"__SHADER__(
282 highp vec3 ToneMap(color) {
283 return clamp(color, 0.0, 1000.0);
284 }
285 )__SHADER__";
286 break;
287 case Key::OUTPUT_TF_ST2084:
288 fs << R"__SHADER__(
289 highp vec3 ToneMap(color) {
290 return color;
291 }
292 )__SHADER__";
293 break;
294 default:
295 fs << R"__SHADER__(
296 highp vec3 ToneMap(color) {
297 const float maxMasteringLumi = 1000.0;
298 const float maxContentLumi = 1000.0;
299 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
300 float maxOutLumi = displayMaxLuminance;
301
302 float nits = color.y;
303
304 // clamp to max input luminance
305 nits = clamp(nits, 0.0, maxInLumi);
306
307 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
308 if (maxInLumi <= maxOutLumi) {
309 nits *= maxOutLumi / maxInLumi;
310 } else {
311 // three control points
312 const float x0 = 10.0;
313 const float y0 = 17.0;
314 float x1 = maxOutLumi * 0.75;
315 float y1 = x1;
316 float x2 = x1 + (maxInLumi - x1) / 2.0;
317 float y2 = y1 + (maxOutLumi - y1) * 0.75;
318
319 // horizontal distances between the last three control points
320 const float h12 = x2 - x1;
321 const float h23 = maxInLumi - x2;
322 // tangents at the last three control points
323 const float m1 = (y2 - y1) / h12;
324 const float m3 = (maxOutLumi - y2) / h23;
325 const float m2 = (m1 + m3) / 2.0;
326
327 if (nits < x0) {
328 // scale [0.0, x0] to [0.0, y0] linearly
329 const float slope = y0 / x0;
330 nits *= slope;
331 } else if (nits < x1) {
332 // scale [x0, x1] to [y0, y1] linearly
333 const float slope = (y1 - y0) / (x1 - x0);
334 nits = y0 + (nits - x0) * slope;
335 } else if (nits < x2) {
336 // scale [x1, x2] to [y1, y2] using Hermite interp
337 float t = (nits - x1) / h12;
338 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
339 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
340 } else {
341 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
342 float t = (nits - x2) / h23;
343 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
344 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
345 }
346 }
347
348 return color * (nits / max(1e-6, color.y));
349 }
350 )__SHADER__";
351 break;
352 }
353 break;
354 default:
355 // TODO(73825729) We need to revert the tone mapping in
356 // hardware composer properly.
357 fs << R"__SHADER__(
358 highp vec3 ToneMap(color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700359 return color;
360 }
361 )__SHADER__";
362 break;
363 }
Peiyong Lin0589adc2018-04-23 17:31:06 -0700364
365 // convert absolute light to relative light.
366 switch (needs.getOutputTF()) {
367 case Key::OUTPUT_TF_ST2084:
368 fs << R"__SHADER__(
369 highp vec3 NormalizeLuminance(color) {
370 return color / 10000.0;
371 }
372 )__SHADER__";
373 break;
374 case Key::OUTPUT_TF_HLG:
375 fs << R"__SHADER__(
376 highp vec3 NormalizeLuminance(color) {
377 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
378 }
379 )__SHADER__";
380 break;
381 default:
382 fs << R"__SHADER__(
383 highp vec3 NormalizeLuminance(color) {
384 return color / displayMaxLuminance;
385 }
386 )__SHADER__";
387 break;
388 }
389
390 if (needs.getInputTF() == needs.getOutputTF() ||
391 (needs.getInputTF() == Key::INPUT_TF_LINEAR &&
392 needs.getOutputTF() == Key::OUTPUT_TF_SRGB) ||
393 (needs.getInputTF() == Key::INPUT_TF_SRGB &&
394 needs.getOutputTF() == Key::OUTPUT_TF_LINEAR)) {
395 fs << R"__SHADER__(
396 highp vec3 OOTF(const highp vec3 color) {
397 return color;
398 }
399 )__SHADER__";
400 } else {
401 fs << R"__SHADER__(
402 highp vec3 OOTF(const highp vec3 color) {
403 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
404 }
405 )__SHADER__";
406 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700407}
408
409// Generate OETF that converts relative display light to signal values,
410// both normalized to [0, 1]
411void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
412 switch (needs.getOutputTF()) {
413 case Key::OUTPUT_TF_SRGB:
414 fs << R"__SHADER__(
415 float OETF_sRGB(const float linear) {
416 return linear <= 0.0031308 ?
417 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
418 }
419
420 vec3 OETF_sRGB(const vec3 linear) {
421 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
422 }
423
424 vec3 OETF(const vec3 linear) {
425 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
426 }
427 )__SHADER__";
428 break;
429 case Key::OUTPUT_TF_ST2084:
430 fs << R"__SHADER__(
431 vec3 OETF(const vec3 linear) {
432 const float m1 = (2610.0 / 4096.0) / 4.0;
433 const float m2 = (2523.0 / 4096.0) * 128.0;
434 const float c1 = (3424.0 / 4096.0);
435 const float c2 = (2413.0 / 4096.0) * 32.0;
436 const float c3 = (2392.0 / 4096.0) * 32.0;
437
438 vec3 tmp = pow(linear, vec3(m1));
439 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
440 return pow(tmp, vec3(m2));
441 }
442 )__SHADER__";
443 break;
444 case Key::OUTPUT_TF_HLG:
445 fs << R"__SHADER__(
446 highp float OETF_channel(const highp float channel) {
447 const highp float a = 0.17883277;
448 const highp float b = 0.28466892;
449 const highp float c = 0.55991073;
450 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
451 a * log(12.0 * channel - b) + c;
452 }
453
454 vec3 OETF(const highp vec3 color) {
455 return vec3(OETF_channel(color.r), OETF_channel(color.g),
456 OETF_channel(color.b));
457 }
458 )__SHADER__";
459 break;
460 default:
461 fs << R"__SHADER__(
462 vec3 OETF(const vec3 linear) {
463 return linear;
464 }
465 )__SHADER__";
466 break;
467 }
468}
469
Mathias Agopian3f844832013-08-07 21:24:32 -0700470String8 ProgramCache::generateVertexShader(const Key& needs) {
471 Formatter vs;
472 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800473 vs << "attribute vec4 texCoords;"
474 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700475 }
476 vs << "attribute vec4 position;"
477 << "uniform mat4 projection;"
478 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800479 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700480 if (needs.isTexturing()) {
481 vs << "outTexCoords = (texture * texCoords).st;";
482 }
483 vs << dedent << "}";
484 return vs.getString();
485}
486
487String8 ProgramCache::generateFragmentShader(const Key& needs) {
488 Formatter fs;
489 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
490 fs << "#extension GL_OES_EGL_image_external : require";
491 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700492
493 // default precision is required-ish in fragment shaders
494 fs << "precision mediump float;";
495
Mathias Agopian3f844832013-08-07 21:24:32 -0700496 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
497 fs << "uniform samplerExternalOES sampler;"
498 << "varying vec2 outTexCoords;";
499 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
500 fs << "uniform sampler2D sampler;"
501 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700502 }
503
504 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700505 fs << "uniform vec4 color;";
506 }
chaviw13fdc492017-06-27 12:40:18 -0700507
Chia-I Wu131d3762018-01-11 14:35:27 -0800508 if (needs.isY410BT2020()) {
509 fs << R"__SHADER__(
510 vec3 convertY410BT2020(const vec3 color) {
511 const vec3 offset = vec3(0.0625, 0.5, 0.5);
512 const mat3 transform = mat3(
513 vec3(1.1678, 1.1678, 1.1678),
514 vec3( 0.0, -0.1878, 2.1481),
515 vec3(1.6836, -0.6523, 0.0));
516 // Y is in G, U is in R, and V is in B
517 return clamp(transform * (color.grb - offset), 0.0, 1.0);
518 }
519 )__SHADER__";
520 }
521
Peiyong Lina296b0c2018-04-30 16:55:29 -0700522 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Linfb069302018-04-25 14:34:31 -0700523 // Currently, only the OOTF of BT2020 PQ needs display maximum luminance.
524 if (needs.getInputTF() == Key::INPUT_TF_ST2084) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700525 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700526 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700527
Peiyong Lina296b0c2018-04-30 16:55:29 -0700528 if (needs.hasInputTransformMatrix()) {
529 fs << "uniform mat3 inputTransformMatrix;";
530 fs << R"__SHADER__(
531 highp vec3 InputTransform(const highp vec3 color) {
532 return inputTransformMatrix * color;
533 }
534 )__SHADER__";
535 } else {
536 fs << R"__SHADER__(
537 highp vec3 InputTransform(const highp vec3 color) {
538 return color;
539 }
540 )__SHADER__";
541 }
542
543 // the transformation from a wider colorspace to a narrower one can
544 // result in >1.0 or <0.0 pixel values
545 if (needs.hasOutputTransformMatrix()) {
546 fs << "uniform mat4 outputTransformMatrix;";
547 fs << R"__SHADER__(
548 highp vec3 OutputTransform(const highp vec3 color) {
549 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
550 }
551 )__SHADER__";
552 } else {
553 fs << R"__SHADER__(
554 highp vec3 OutputTransform(const highp vec3 color) {
555 return clamp(color, 0.0, 1.0);
556 }
557 )__SHADER__";
558 }
559
Peiyong Lin2542cb02018-04-30 17:29:53 -0700560 generateEOTF(fs, needs);
561 generateOOTF(fs, needs);
562 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700563 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800564
Mathias Agopian3f844832013-08-07 21:24:32 -0700565 fs << "void main(void) {" << indent;
566 if (needs.isTexturing()) {
567 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800568 if (needs.isY410BT2020()) {
569 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
570 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700571 } else {
chaviw13fdc492017-06-27 12:40:18 -0700572 fs << "gl_FragColor.rgb = color.rgb;";
573 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700574 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700575 if (needs.isOpaque()) {
576 fs << "gl_FragColor.a = 1.0;";
577 }
chaviw13fdc492017-06-27 12:40:18 -0700578 if (needs.hasAlpha()) {
579 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700580 if (needs.isPremultiplied()) {
581 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700582 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700583 } else {
chaviw13fdc492017-06-27 12:40:18 -0700584 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700585 }
586 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700587
Peiyong Lina296b0c2018-04-30 16:55:29 -0700588 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700589 if (!needs.isOpaque() && needs.isPremultiplied()) {
590 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700591 // avoid divide by 0 by adding 0.5/256 to the alpha channel
592 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700593 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700594 fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700595 if (!needs.isOpaque() && needs.isPremultiplied()) {
596 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700597 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700598 }
599 }
600
Mathias Agopian3f844832013-08-07 21:24:32 -0700601 fs << dedent << "}";
602 return fs.getString();
603}
604
605Program* ProgramCache::generateProgram(const Key& needs) {
606 // vertex shader
607 String8 vs = generateVertexShader(needs);
608
609 // fragment shader
610 String8 fs = generateFragmentShader(needs);
611
612 Program* program = new Program(needs, vs.string(), fs.string());
613 return program;
614}
615
616void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700617 // generate the key for the shader based on the description
618 Key needs(computeKey(description));
619
Chia-I Wub027f802017-11-29 14:00:52 -0800620 // look-up the program in the cache
Mathias Agopian3f844832013-08-07 21:24:32 -0700621 Program* program = mCache.valueFor(needs);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800622 if (program == nullptr) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700623 // we didn't find our program, so generate one...
624 nsecs_t time = -systemTime();
625 program = generateProgram(needs);
626 mCache.add(needs, program);
627 time += systemTime();
628
Chia-I Wub027f802017-11-29 14:00:52 -0800629 // ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)",
Mathias Agopian3f844832013-08-07 21:24:32 -0700630 // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size());
631 }
632
633 // here we have a suitable program for this description
634 if (program->isValid()) {
635 program->use();
636 program->setUniforms(description);
637 }
638}
639
Mathias Agopian3f844832013-08-07 21:24:32 -0700640} /* namespace android */