1 /*
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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 
19 #include "ProgramCache.h"
20 
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <log/log.h>
24 #include <renderengine/private/Description.h>
25 #include <utils/String8.h>
26 #include <utils/Trace.h>
27 #include "Program.h"
28 
29 ANDROID_SINGLETON_STATIC_INSTANCE(android::renderengine::gl::ProgramCache)
30 
31 namespace android {
32 namespace renderengine {
33 namespace gl {
34 
35 /*
36  * A simple formatter class to automatically add the endl and
37  * manage the indentation.
38  */
39 
40 class Formatter;
41 static Formatter& indent(Formatter& f);
42 static Formatter& dedent(Formatter& f);
43 
44 class Formatter {
45     String8 mString;
46     int mIndent;
47     typedef Formatter& (*FormaterManipFunc)(Formatter&);
48     friend Formatter& indent(Formatter& f);
49     friend Formatter& dedent(Formatter& f);
50 
51 public:
Formatter()52     Formatter() : mIndent(0) {}
53 
getString() const54     String8 getString() const { return mString; }
55 
operator <<(Formatter & out,const char * in)56     friend Formatter& operator<<(Formatter& out, const char* in) {
57         for (int i = 0; i < out.mIndent; i++) {
58             out.mString.append("    ");
59         }
60         out.mString.append(in);
61         out.mString.append("\n");
62         return out;
63     }
operator <<(Formatter & out,const String8 & in)64     friend inline Formatter& operator<<(Formatter& out, const String8& in) {
65         return operator<<(out, in.c_str());
66     }
operator <<(Formatter & to,FormaterManipFunc func)67     friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) {
68         return (*func)(to);
69     }
70 };
indent(Formatter & f)71 Formatter& indent(Formatter& f) {
72     f.mIndent++;
73     return f;
74 }
dedent(Formatter & f)75 Formatter& dedent(Formatter& f) {
76     f.mIndent--;
77     return f;
78 }
79 
primeCache(EGLContext context,bool useColorManagement,bool toneMapperShaderOnly)80 void ProgramCache::primeCache(
81         EGLContext context, bool useColorManagement, bool toneMapperShaderOnly) {
82     auto& cache = mCaches[context];
83     uint32_t shaderCount = 0;
84 
85     if (toneMapperShaderOnly) {
86         Key shaderKey;
87         // base settings used by HDR->SDR tonemap only
88         shaderKey.set(Key::BLEND_MASK | Key::INPUT_TRANSFORM_MATRIX_MASK |
89                       Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::OUTPUT_TF_MASK |
90                       Key::OPACITY_MASK | Key::ALPHA_MASK |
91                       Key::ROUNDED_CORNERS_MASK | Key::TEXTURE_MASK,
92                       Key::BLEND_NORMAL | Key::INPUT_TRANSFORM_MATRIX_ON |
93                       Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::OUTPUT_TF_SRGB |
94                       Key::OPACITY_OPAQUE | Key::ALPHA_EQ_ONE |
95                       Key::ROUNDED_CORNERS_OFF | Key::TEXTURE_EXT);
96         for (int i = 0; i < 4; i++) {
97             // Cache input transfer for HLG & ST2084
98             shaderKey.set(Key::INPUT_TF_MASK, (i & 1) ?
99                     Key::INPUT_TF_HLG : Key::INPUT_TF_ST2084);
100 
101             // Cache Y410 input on or off
102             shaderKey.set(Key::Y410_BT2020_MASK, (i & 2) ?
103                     Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
104             if (cache.count(shaderKey) == 0) {
105                 cache.emplace(shaderKey, generateProgram(shaderKey));
106                 shaderCount++;
107             }
108         }
109         return;
110     }
111 
112     uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK
113         | Key::ROUNDED_CORNERS_MASK;
114     // Prime the cache for all combinations of the above masks,
115     // leaving off the experimental color matrix mask options.
116 
117     nsecs_t timeBefore = systemTime();
118     for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
119         Key shaderKey;
120         shaderKey.set(keyMask, keyVal);
121         uint32_t tex = shaderKey.getTextureTarget();
122         if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
123             continue;
124         }
125         if (cache.count(shaderKey) == 0) {
126             cache.emplace(shaderKey, generateProgram(shaderKey));
127             shaderCount++;
128         }
129     }
130 
131     // Prime for sRGB->P3 conversion
132     if (useColorManagement) {
133         Key shaderKey;
134         shaderKey.set(Key::BLEND_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK | Key::INPUT_TF_MASK |
135                               Key::OUTPUT_TF_MASK,
136                       Key::BLEND_PREMULT | Key::OUTPUT_TRANSFORM_MATRIX_ON | Key::INPUT_TF_SRGB |
137                               Key::OUTPUT_TF_SRGB);
138         for (int i = 0; i < 16; i++) {
139             shaderKey.set(Key::OPACITY_MASK,
140                           (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
141             shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
142 
143             // Cache rounded corners
144             shaderKey.set(Key::ROUNDED_CORNERS_MASK,
145                           (i & 4) ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF);
146 
147             // Cache texture off option for window transition
148             shaderKey.set(Key::TEXTURE_MASK, (i & 8) ? Key::TEXTURE_EXT : Key::TEXTURE_OFF);
149             if (cache.count(shaderKey) == 0) {
150                 cache.emplace(shaderKey, generateProgram(shaderKey));
151                 shaderCount++;
152             }
153         }
154     }
155 
156     nsecs_t timeAfter = systemTime();
157     float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
158     ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
159 }
160 
computeKey(const Description & description)161 ProgramCache::Key ProgramCache::computeKey(const Description& description) {
162     Key needs;
163     needs.set(Key::TEXTURE_MASK,
164               !description.textureEnabled
165                       ? Key::TEXTURE_OFF
166                       : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
167                               ? Key::TEXTURE_EXT
168                               : description.texture.getTextureTarget() == GL_TEXTURE_2D
169                                       ? Key::TEXTURE_2D
170                                       : Key::TEXTURE_OFF)
171             .set(Key::ALPHA_MASK, (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
172             .set(Key::BLEND_MASK,
173                  description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
174             .set(Key::OPACITY_MASK,
175                  description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
176             .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
177                  description.hasInputTransformMatrix() ? Key::INPUT_TRANSFORM_MATRIX_ON
178                                                        : Key::INPUT_TRANSFORM_MATRIX_OFF)
179             .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
180                  description.hasOutputTransformMatrix() || description.hasColorMatrix()
181                          ? Key::OUTPUT_TRANSFORM_MATRIX_ON
182                          : Key::OUTPUT_TRANSFORM_MATRIX_OFF)
183             .set(Key::ROUNDED_CORNERS_MASK,
184                  description.cornerRadius > 0 ? Key::ROUNDED_CORNERS_ON : Key::ROUNDED_CORNERS_OFF)
185             .set(Key::SHADOW_MASK, description.drawShadows ? Key::SHADOW_ON : Key::SHADOW_OFF);
186     needs.set(Key::Y410_BT2020_MASK,
187               description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
188 
189     if (needs.hasTransformMatrix() ||
190         (description.inputTransferFunction != description.outputTransferFunction)) {
191         switch (description.inputTransferFunction) {
192             case Description::TransferFunction::LINEAR:
193             default:
194                 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
195                 break;
196             case Description::TransferFunction::SRGB:
197                 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
198                 break;
199             case Description::TransferFunction::ST2084:
200                 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
201                 break;
202             case Description::TransferFunction::HLG:
203                 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
204                 break;
205         }
206 
207         switch (description.outputTransferFunction) {
208             case Description::TransferFunction::LINEAR:
209             default:
210                 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
211                 break;
212             case Description::TransferFunction::SRGB:
213                 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
214                 break;
215             case Description::TransferFunction::ST2084:
216                 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
217                 break;
218             case Description::TransferFunction::HLG:
219                 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
220                 break;
221         }
222     }
223 
224     return needs;
225 }
226 
227 // Generate EOTF that converts signal values to relative display light,
228 // both normalized to [0, 1].
generateEOTF(Formatter & fs,const Key & needs)229 void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
230     switch (needs.getInputTF()) {
231         case Key::INPUT_TF_SRGB:
232             fs << R"__SHADER__(
233                 float EOTF_sRGB(float srgb) {
234                     return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
235                 }
236 
237                 vec3 EOTF_sRGB(const vec3 srgb) {
238                     return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
239                 }
240 
241                 vec3 EOTF(const vec3 srgb) {
242                     return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
243                 }
244             )__SHADER__";
245             break;
246         case Key::INPUT_TF_ST2084:
247             fs << R"__SHADER__(
248                 vec3 EOTF(const highp vec3 color) {
249                     const highp float m1 = (2610.0 / 4096.0) / 4.0;
250                     const highp float m2 = (2523.0 / 4096.0) * 128.0;
251                     const highp float c1 = (3424.0 / 4096.0);
252                     const highp float c2 = (2413.0 / 4096.0) * 32.0;
253                     const highp float c3 = (2392.0 / 4096.0) * 32.0;
254 
255                     highp vec3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / vec3(m2));
256                     tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
257                     return pow(tmp, 1.0 / vec3(m1));
258                 }
259             )__SHADER__";
260             break;
261         case Key::INPUT_TF_HLG:
262             fs << R"__SHADER__(
263                 highp float EOTF_channel(const highp float channel) {
264                     const highp float a = 0.17883277;
265                     const highp float b = 0.28466892;
266                     const highp float c = 0.55991073;
267                     return channel <= 0.5 ? channel * channel / 3.0 :
268                             (exp((channel - c) / a) + b) / 12.0;
269                 }
270 
271                 vec3 EOTF(const highp vec3 color) {
272                     return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
273                             EOTF_channel(color.b));
274                 }
275             )__SHADER__";
276             break;
277         default:
278             fs << R"__SHADER__(
279                 vec3 EOTF(const vec3 linear) {
280                     return linear;
281                 }
282             )__SHADER__";
283             break;
284     }
285 }
286 
generateToneMappingProcess(Formatter & fs,const Key & needs)287 void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
288     // Convert relative light to absolute light.
289     switch (needs.getInputTF()) {
290         case Key::INPUT_TF_ST2084:
291             fs << R"__SHADER__(
292                 highp vec3 ScaleLuminance(highp vec3 color) {
293                     return color * 10000.0;
294                 }
295             )__SHADER__";
296             break;
297         case Key::INPUT_TF_HLG:
298             fs << R"__SHADER__(
299                 highp vec3 ScaleLuminance(highp vec3 color) {
300                     // The formula is:
301                     // alpha * pow(Y, gamma - 1.0) * color + beta;
302                     // where alpha is displayMaxLuminance, gamma is 1.2, beta is 0.0.
303                     return color * displayMaxLuminance * pow(color.y, 0.2);
304                 }
305             )__SHADER__";
306             break;
307         default:
308             fs << R"__SHADER__(
309                 highp vec3 ScaleLuminance(highp vec3 color) {
310                     return color * displayMaxLuminance;
311                 }
312             )__SHADER__";
313             break;
314     }
315 
316     // Tone map absolute light to display luminance range.
317     switch (needs.getInputTF()) {
318         case Key::INPUT_TF_ST2084:
319             switch (needs.getOutputTF()) {
320                 case Key::OUTPUT_TF_HLG:
321                     // Right now when mixed PQ and HLG contents are presented,
322                     // HLG content will always be converted to PQ. However, for
323                     // completeness, we simply clamp the value to [0.0, 1000.0].
324                     fs << R"__SHADER__(
325                         highp vec3 ToneMap(highp vec3 color) {
326                             return clamp(color, 0.0, 1000.0);
327                         }
328                     )__SHADER__";
329                     break;
330                 case Key::OUTPUT_TF_ST2084:
331                     fs << R"__SHADER__(
332                         highp vec3 ToneMap(highp vec3 color) {
333                             return color;
334                         }
335                     )__SHADER__";
336                     break;
337                 default:
338                     fs << R"__SHADER__(
339                         highp vec3 ToneMap(highp vec3 color) {
340                             float maxMasteringLumi = maxMasteringLuminance;
341                             float maxContentLumi = maxContentLuminance;
342                             float maxInLumi = min(maxMasteringLumi, maxContentLumi);
343                             float maxOutLumi = displayMaxLuminance;
344 
345                             float nits = color.y;
346 
347                             // clamp to max input luminance
348                             nits = clamp(nits, 0.0, maxInLumi);
349 
350                             // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
351                             if (maxInLumi <= maxOutLumi) {
352                                 return color * (maxOutLumi / maxInLumi);
353                             } else {
354                                 // three control points
355                                 const float x0 = 10.0;
356                                 const float y0 = 17.0;
357                                 float x1 = maxOutLumi * 0.75;
358                                 float y1 = x1;
359                                 float x2 = x1 + (maxInLumi - x1) / 2.0;
360                                 float y2 = y1 + (maxOutLumi - y1) * 0.75;
361 
362                                 // horizontal distances between the last three control points
363                                 float h12 = x2 - x1;
364                                 float h23 = maxInLumi - x2;
365                                 // tangents at the last three control points
366                                 float m1 = (y2 - y1) / h12;
367                                 float m3 = (maxOutLumi - y2) / h23;
368                                 float m2 = (m1 + m3) / 2.0;
369 
370                                 if (nits < x0) {
371                                     // scale [0.0, x0] to [0.0, y0] linearly
372                                     float slope = y0 / x0;
373                                     return color * slope;
374                                 } else if (nits < x1) {
375                                     // scale [x0, x1] to [y0, y1] linearly
376                                     // Use highp since some compilers may do this
377                                     // operation as reciprocal multiplication with
378                                     // re-association that could exceed the range
379                                     // of mediump float.
380                                     highp float slope = (y1 - y0) / (x1 - x0);
381                                     nits = y0 + (nits - x0) * slope;
382                                 } else if (nits < x2) {
383                                     // scale [x1, x2] to [y1, y2] using Hermite interp
384                                     float t = (nits - x1) / h12;
385                                     nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
386                                             (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
387                                 } else {
388                                     // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
389                                     float t = (nits - x2) / h23;
390                                     nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
391                                             (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
392                                 }
393                             }
394 
395                             // color.y is greater than x0 and is thus non-zero
396                             return color * (nits / color.y);
397                         }
398                     )__SHADER__";
399                     break;
400             }
401             break;
402         case Key::INPUT_TF_HLG:
403             // HLG OOTF is already applied as part of ScaleLuminance
404             fs << R"__SHADER__(
405                  highp vec3 ToneMap(highp vec3 color) {
406                      return color;
407                  }
408              )__SHADER__";
409             break;
410         default:
411             // inverse tone map; the output luminance can be up to maxOutLumi.
412             fs << R"__SHADER__(
413                 highp vec3 ToneMap(highp vec3 color) {
414                     const float maxOutLumi = 3000.0;
415 
416                     const float x0 = 5.0;
417                     const float y0 = 2.5;
418                     float x1 = displayMaxLuminance * 0.7;
419                     float y1 = maxOutLumi * 0.15;
420                     float x2 = displayMaxLuminance * 0.9;
421                     float y2 = maxOutLumi * 0.45;
422                     float x3 = displayMaxLuminance;
423                     float y3 = maxOutLumi;
424 
425                     float c1 = y1 / 3.0;
426                     float c2 = y2 / 2.0;
427                     float c3 = y3 / 1.5;
428 
429                     float nits = color.y;
430 
431                     float scale;
432                     if (nits <= x0) {
433                         // scale [0.0, x0] to [0.0, y0] linearly
434                         const float slope = y0 / x0;
435                         return color * slope;
436                     } else if (nits <= x1) {
437                         // scale [x0, x1] to [y0, y1] using a curve
438                         float t = (nits - x0) / (x1 - x0);
439                         nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
440                     } else if (nits <= x2) {
441                         // scale [x1, x2] to [y1, y2] using a curve
442                         float t = (nits - x1) / (x2 - x1);
443                         nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
444                     } else {
445                         // scale [x2, x3] to [y2, y3] using a curve
446                         float t = (nits - x2) / (x3 - x2);
447                         nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
448                     }
449 
450                     // color.y is greater than x0 and is thus non-zero
451                     return color * (nits / color.y);
452                 }
453             )__SHADER__";
454             break;
455     }
456 
457     // convert absolute light to relative light.
458     switch (needs.getOutputTF()) {
459         case Key::OUTPUT_TF_ST2084:
460             fs << R"__SHADER__(
461                 highp vec3 NormalizeLuminance(highp vec3 color) {
462                     return color / 10000.0;
463                 }
464             )__SHADER__";
465             break;
466         case Key::OUTPUT_TF_HLG:
467             fs << R"__SHADER__(
468                 highp vec3 NormalizeLuminance(highp vec3 color) {
469                     return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
470                 }
471             )__SHADER__";
472             break;
473         default:
474             fs << R"__SHADER__(
475                 highp vec3 NormalizeLuminance(highp vec3 color) {
476                     return color / displayMaxLuminance;
477                 }
478             )__SHADER__";
479             break;
480     }
481 }
482 
483 // Generate OOTF that modifies the relative scence light to relative display light.
generateOOTF(Formatter & fs,const ProgramCache::Key & needs)484 void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
485     if (!needs.needsToneMapping()) {
486         fs << R"__SHADER__(
487             highp vec3 OOTF(const highp vec3 color) {
488                 return color;
489             }
490         )__SHADER__";
491     } else {
492         generateToneMappingProcess(fs, needs);
493         fs << R"__SHADER__(
494             highp vec3 OOTF(const highp vec3 color) {
495                 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
496             }
497         )__SHADER__";
498     }
499 }
500 
501 // Generate OETF that converts relative display light to signal values,
502 // both normalized to [0, 1]
generateOETF(Formatter & fs,const Key & needs)503 void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
504     switch (needs.getOutputTF()) {
505         case Key::OUTPUT_TF_SRGB:
506             fs << R"__SHADER__(
507                 float OETF_sRGB(const float linear) {
508                     return linear <= 0.0031308 ?
509                             linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
510                 }
511 
512                 vec3 OETF_sRGB(const vec3 linear) {
513                     return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
514                 }
515 
516                 vec3 OETF(const vec3 linear) {
517                     return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
518                 }
519             )__SHADER__";
520             break;
521         case Key::OUTPUT_TF_ST2084:
522             fs << R"__SHADER__(
523                 vec3 OETF(const vec3 linear) {
524                     const highp float m1 = (2610.0 / 4096.0) / 4.0;
525                     const highp float m2 = (2523.0 / 4096.0) * 128.0;
526                     const highp float c1 = (3424.0 / 4096.0);
527                     const highp float c2 = (2413.0 / 4096.0) * 32.0;
528                     const highp float c3 = (2392.0 / 4096.0) * 32.0;
529 
530                     highp vec3 tmp = pow(linear, vec3(m1));
531                     tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
532                     return pow(tmp, vec3(m2));
533                 }
534             )__SHADER__";
535             break;
536         case Key::OUTPUT_TF_HLG:
537             fs << R"__SHADER__(
538                 highp float OETF_channel(const highp float channel) {
539                     const highp float a = 0.17883277;
540                     const highp float b = 0.28466892;
541                     const highp float c = 0.55991073;
542                     return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
543                             a * log(12.0 * channel - b) + c;
544                 }
545 
546                 vec3 OETF(const highp vec3 color) {
547                     return vec3(OETF_channel(color.r), OETF_channel(color.g),
548                             OETF_channel(color.b));
549                 }
550             )__SHADER__";
551             break;
552         default:
553             fs << R"__SHADER__(
554                 vec3 OETF(const vec3 linear) {
555                     return linear;
556                 }
557             )__SHADER__";
558             break;
559     }
560 }
561 
generateVertexShader(const Key & needs)562 String8 ProgramCache::generateVertexShader(const Key& needs) {
563     Formatter vs;
564     if (needs.hasTextureCoords()) {
565         vs << "attribute vec4 texCoords;"
566            << "varying vec2 outTexCoords;";
567     }
568     if (needs.hasRoundedCorners()) {
569         vs << "attribute lowp vec4 cropCoords;";
570         vs << "varying lowp vec2 outCropCoords;";
571     }
572     if (needs.drawShadows()) {
573         vs << "attribute lowp vec4 shadowColor;";
574         vs << "varying lowp vec4 outShadowColor;";
575         vs << "attribute lowp vec4 shadowParams;";
576         vs << "varying lowp vec3 outShadowParams;";
577     }
578     vs << "attribute vec4 position;"
579        << "uniform mat4 projection;"
580        << "uniform mat4 texture;"
581        << "void main(void) {" << indent << "gl_Position = projection * position;";
582     if (needs.hasTextureCoords()) {
583         vs << "outTexCoords = (texture * texCoords).st;";
584     }
585     if (needs.hasRoundedCorners()) {
586         vs << "outCropCoords = cropCoords.st;";
587     }
588     if (needs.drawShadows()) {
589         vs << "outShadowColor = shadowColor;";
590         vs << "outShadowParams = shadowParams.xyz;";
591     }
592     vs << dedent << "}";
593     return vs.getString();
594 }
595 
generateFragmentShader(const Key & needs)596 String8 ProgramCache::generateFragmentShader(const Key& needs) {
597     Formatter fs;
598     if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
599         fs << "#extension GL_OES_EGL_image_external : require";
600     }
601 
602     // default precision is required-ish in fragment shaders
603     fs << "precision mediump float;";
604 
605     if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
606         fs << "uniform samplerExternalOES sampler;";
607     } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
608         fs << "uniform sampler2D sampler;";
609     }
610 
611     if (needs.hasTextureCoords()) {
612         fs << "varying vec2 outTexCoords;";
613     }
614 
615     if (needs.hasRoundedCorners()) {
616         // Rounded corners implementation using a signed distance function.
617         fs << R"__SHADER__(
618             uniform float cornerRadius;
619             uniform vec2 cropCenter;
620             varying vec2 outCropCoords;
621 
622             /**
623              * This function takes the current crop coordinates and calculates an alpha value based
624              * on the corner radius and distance from the crop center.
625              */
626             float applyCornerRadius(vec2 cropCoords)
627             {
628                 vec2 position = cropCoords - cropCenter;
629                 // Scale down the dist vector here, as otherwise large corner
630                 // radii can cause floating point issues when computing the norm
631                 vec2 dist = (abs(position) - cropCenter + vec2(cornerRadius)) / 16.0;
632                 // Once we've found the norm, then scale back up.
633                 float plane = length(max(dist, vec2(0.0))) * 16.0;
634                 return 1.0 - clamp(plane - cornerRadius, 0.0, 1.0);
635             }
636             )__SHADER__";
637     }
638 
639     if (needs.drawShadows()) {
640         fs << R"__SHADER__(
641             varying lowp vec4 outShadowColor;
642             varying lowp vec3 outShadowParams;
643 
644             /**
645              * Returns the shadow color.
646              */
647             vec4 getShadowColor()
648             {
649                 lowp float d = length(outShadowParams.xy);
650                 vec2 uv = vec2(outShadowParams.z * (1.0 - d), 0.5);
651                 lowp float factor = texture2D(sampler, uv).a;
652                 return outShadowColor * factor;
653             }
654             )__SHADER__";
655     }
656 
657     if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
658         fs << "uniform vec4 color;";
659     }
660 
661     if (needs.isY410BT2020()) {
662         fs << R"__SHADER__(
663             vec3 convertY410BT2020(const vec3 color) {
664                 const vec3 offset = vec3(0.0625, 0.5, 0.5);
665                 const mat3 transform = mat3(
666                     vec3(1.1678,  1.1678, 1.1678),
667                     vec3(   0.0, -0.1878, 2.1481),
668                     vec3(1.6836, -0.6523,   0.0));
669                 // Y is in G, U is in R, and V is in B
670                 return clamp(transform * (color.grb - offset), 0.0, 1.0);
671             }
672             )__SHADER__";
673     }
674 
675     if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
676         if (needs.needsToneMapping()) {
677             fs << "uniform float displayMaxLuminance;";
678             fs << "uniform float maxMasteringLuminance;";
679             fs << "uniform float maxContentLuminance;";
680         }
681 
682         if (needs.hasInputTransformMatrix()) {
683             fs << "uniform mat4 inputTransformMatrix;";
684             fs << R"__SHADER__(
685                 highp vec3 InputTransform(const highp vec3 color) {
686                     return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
687                 }
688             )__SHADER__";
689         } else {
690             fs << R"__SHADER__(
691                 highp vec3 InputTransform(const highp vec3 color) {
692                     return color;
693                 }
694             )__SHADER__";
695         }
696 
697         // the transformation from a wider colorspace to a narrower one can
698         // result in >1.0 or <0.0 pixel values
699         if (needs.hasOutputTransformMatrix()) {
700             fs << "uniform mat4 outputTransformMatrix;";
701             fs << R"__SHADER__(
702                 highp vec3 OutputTransform(const highp vec3 color) {
703                     return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
704                 }
705             )__SHADER__";
706         } else {
707             fs << R"__SHADER__(
708                 highp vec3 OutputTransform(const highp vec3 color) {
709                     return clamp(color, 0.0, 1.0);
710                 }
711             )__SHADER__";
712         }
713 
714         generateEOTF(fs, needs);
715         generateOOTF(fs, needs);
716         generateOETF(fs, needs);
717     }
718 
719     fs << "void main(void) {" << indent;
720     if (needs.drawShadows()) {
721         fs << "gl_FragColor = getShadowColor();";
722     } else {
723         if (needs.isTexturing()) {
724             fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
725             if (needs.isY410BT2020()) {
726                 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
727             }
728         } else {
729             fs << "gl_FragColor.rgb = color.rgb;";
730             fs << "gl_FragColor.a = 1.0;";
731         }
732         if (needs.isOpaque()) {
733             fs << "gl_FragColor.a = 1.0;";
734         }
735         if (needs.hasAlpha()) {
736             // modulate the current alpha value with alpha set
737             if (needs.isPremultiplied()) {
738                 // ... and the color too if we're premultiplied
739                 fs << "gl_FragColor *= color.a;";
740             } else {
741                 fs << "gl_FragColor.a *= color.a;";
742             }
743         }
744     }
745 
746     if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
747         if (!needs.isOpaque() && needs.isPremultiplied()) {
748             // un-premultiply if needed before linearization
749             // avoid divide by 0 by adding 0.5/256 to the alpha channel
750             fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
751         }
752         fs << "gl_FragColor.rgb = "
753               "OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
754         if (!needs.isOpaque() && needs.isPremultiplied()) {
755             // and re-premultiply if needed after gamma correction
756             fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
757         }
758     }
759 
760     if (needs.hasRoundedCorners()) {
761         if (needs.isPremultiplied()) {
762             fs << "gl_FragColor *= vec4(applyCornerRadius(outCropCoords));";
763         } else {
764             fs << "gl_FragColor.a *= applyCornerRadius(outCropCoords);";
765         }
766     }
767 
768     fs << dedent << "}";
769     return fs.getString();
770 }
771 
generateProgram(const Key & needs)772 std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
773     ATRACE_CALL();
774 
775     // vertex shader
776     String8 vs = generateVertexShader(needs);
777 
778     // fragment shader
779     String8 fs = generateFragmentShader(needs);
780 
781     return std::make_unique<Program>(needs, vs.c_str(), fs.c_str());
782 }
783 
useProgram(EGLContext context,const Description & description)784 void ProgramCache::useProgram(EGLContext context, const Description& description) {
785     // generate the key for the shader based on the description
786     Key needs(computeKey(description));
787 
788     // look-up the program in the cache
789     auto& cache = mCaches[context];
790     auto it = cache.find(needs);
791     if (it == cache.end()) {
792         // we didn't find our program, so generate one...
793         nsecs_t time = systemTime();
794         it = cache.emplace(needs, generateProgram(needs)).first;
795         time = systemTime() - time;
796 
797         ALOGV(">>> generated new program for context %p: needs=%08X, time=%u ms (%zu programs)",
798               context, needs.mKey, uint32_t(ns2ms(time)), cache.size());
799     }
800 
801     // here we have a suitable program for this description
802     std::unique_ptr<Program>& program = it->second;
803     if (program->isValid()) {
804         program->use();
805         program->setUniforms(description);
806     }
807 }
808 
809 } // namespace gl
810 } // namespace renderengine
811 } // namespace android
812