blob: d1887ee925809ee9db8da4c9024f3f2666fe270c [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)
128 .set(Key::COLOR_MATRIX_MASK,
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800129 description.mColorMatrixEnabled ? Key::COLOR_MATRIX_ON : Key::COLOR_MATRIX_OFF);
130
Chia-I Wu131d3762018-01-11 14:35:27 -0800131 needs.set(Key::Y410_BT2020_MASK,
132 description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
133
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800134 if (needs.hasColorMatrix()) {
135 switch (description.mInputTransferFunction) {
136 case Description::TransferFunction::LINEAR:
137 default:
138 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
139 break;
140 case Description::TransferFunction::SRGB:
141 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
142 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800143 case Description::TransferFunction::ST2084:
144 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
145 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700146 case Description::TransferFunction::HLG:
147 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
148 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800149 }
150
151 switch (description.mOutputTransferFunction) {
152 case Description::TransferFunction::LINEAR:
153 default:
154 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
155 break;
156 case Description::TransferFunction::SRGB:
157 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
158 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800159 case Description::TransferFunction::ST2084:
160 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
161 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700162 case Description::TransferFunction::HLG:
163 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
164 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800165 }
166 }
167
Mathias Agopian3f844832013-08-07 21:24:32 -0700168 return needs;
169}
170
171String8 ProgramCache::generateVertexShader(const Key& needs) {
172 Formatter vs;
173 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800174 vs << "attribute vec4 texCoords;"
175 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700176 }
177 vs << "attribute vec4 position;"
178 << "uniform mat4 projection;"
179 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800180 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700181 if (needs.isTexturing()) {
182 vs << "outTexCoords = (texture * texCoords).st;";
183 }
184 vs << dedent << "}";
185 return vs.getString();
186}
187
188String8 ProgramCache::generateFragmentShader(const Key& needs) {
189 Formatter fs;
190 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
191 fs << "#extension GL_OES_EGL_image_external : require";
192 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700193
194 // default precision is required-ish in fragment shaders
195 fs << "precision mediump float;";
196
Mathias Agopian3f844832013-08-07 21:24:32 -0700197 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
198 fs << "uniform samplerExternalOES sampler;"
199 << "varying vec2 outTexCoords;";
200 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
201 fs << "uniform sampler2D sampler;"
202 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700203 }
204
205 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700206 fs << "uniform vec4 color;";
207 }
chaviw13fdc492017-06-27 12:40:18 -0700208
Chia-I Wu131d3762018-01-11 14:35:27 -0800209 if (needs.isY410BT2020()) {
210 fs << R"__SHADER__(
211 vec3 convertY410BT2020(const vec3 color) {
212 const vec3 offset = vec3(0.0625, 0.5, 0.5);
213 const mat3 transform = mat3(
214 vec3(1.1678, 1.1678, 1.1678),
215 vec3( 0.0, -0.1878, 2.1481),
216 vec3(1.6836, -0.6523, 0.0));
217 // Y is in G, U is in R, and V is in B
218 return clamp(transform * (color.grb - offset), 0.0, 1.0);
219 }
220 )__SHADER__";
221 }
222
Mathias Agopianff2ed702013-09-01 21:36:12 -0700223 if (needs.hasColorMatrix()) {
224 fs << "uniform mat4 colorMatrix;";
Romain Guy88d37dd2017-05-26 17:57:05 -0700225
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700226 // Generate EOTF that converts signal values to relative display light,
227 // both normalized to [0, 1].
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800228 switch (needs.getInputTF()) {
229 case Key::INPUT_TF_LINEAR:
230 default:
231 fs << R"__SHADER__(
232 vec3 EOTF(const vec3 linear) {
233 return linear;
234 }
235 )__SHADER__";
236 break;
237 case Key::INPUT_TF_SRGB:
238 fs << R"__SHADER__(
239 float EOTF_sRGB(float srgb) {
240 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
241 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700242
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800243 vec3 EOTF_sRGB(const vec3 srgb) {
244 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
245 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700246
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800247 vec3 EOTF(const vec3 srgb) {
248 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
249 }
250 )__SHADER__";
251 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800252 case Key::INPUT_TF_ST2084:
253 fs << R"__SHADER__(
254 vec3 EOTF(const highp vec3 color) {
255 const highp float m1 = (2610.0 / 4096.0) / 4.0;
256 const highp float m2 = (2523.0 / 4096.0) * 128.0;
257 const highp float c1 = (3424.0 / 4096.0);
258 const highp float c2 = (2413.0 / 4096.0) * 32.0;
259 const highp float c3 = (2392.0 / 4096.0) * 32.0;
260
261 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
262 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
263 return pow(tmp, 1.0 / vec3(m1));
264 }
265 )__SHADER__";
266 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700267 case Key::INPUT_TF_HLG:
268 fs << R"__SHADER__(
269 highp float EOTF_channel(const highp float channel) {
270 const highp float a = 0.17883277;
271 const highp float b = 0.28466892;
272 const highp float c = 0.55991073;
273 return channel <= 0.5 ? channel * channel / 3.0 :
274 (exp((channel - c) / a) + b) / 12.0;
275 }
276
277 vec3 EOTF(const highp vec3 color) {
278 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
279 EOTF_channel(color.b));
280 }
281 )__SHADER__";
282 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800283 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700284
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700285 fs << R"__SHADER__(
286 highp float CalculateY(const highp vec3 color) {
287 // BT2020 standard uses the unadjusted KR = 0.2627,
288 // KB = 0.0593 luminance interpretation for RGB conversion.
289 return color.r * 0.262700 + color.g * 0.677998 +
290 color.b * 0.059302;
291 }
292 )__SHADER__";
293
294 // Generate OOTF that modifies the relative display light.
295 switch(needs.getInputTF()) {
296 case Key::INPUT_TF_ST2084:
297 fs << R"__SHADER__(
298 highp vec3 OOTF(const highp vec3 color) {
299 const float maxLumi = 10000.0;
300 const float maxMasteringLumi = 1000.0;
301 const float maxContentLumi = 1000.0;
302 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
303 const float maxOutLumi = 500.0;
304
305 // Calculate Y value in XYZ color space.
306 float colorY = CalculateY(color);
307
308 // convert to nits first
309 float nits = colorY * maxLumi;
310
311 // clamp to max input luminance
312 nits = clamp(nits, 0.0, maxInLumi);
313
314 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
315 if (maxInLumi <= maxOutLumi) {
316 nits *= maxOutLumi / maxInLumi;
317 } else {
318 // three control points
319 const float x0 = 10.0;
320 const float y0 = 17.0;
321 const float x1 = maxOutLumi * 0.75;
322 const float y1 = x1;
323 const float x2 = x1 + (maxInLumi - x1) / 2.0;
324 const float y2 = y1 + (maxOutLumi - y1) * 0.75;
325
326 // horizontal distances between the last three control points
327 const float h12 = x2 - x1;
328 const float h23 = maxInLumi - x2;
329 // tangents at the last three control points
330 const float m1 = (y2 - y1) / h12;
331 const float m3 = (maxOutLumi - y2) / h23;
332 const float m2 = (m1 + m3) / 2.0;
333
334 if (nits < x0) {
335 // scale [0.0, x0] to [0.0, y0] linearly
336 const float slope = y0 / x0;
337 nits *= slope;
338 } else if (nits < x1) {
339 // scale [x0, x1] to [y0, y1] linearly
340 const float slope = (y1 - y0) / (x1 - x0);
341 nits = y0 + (nits - x0) * slope;
342 } else if (nits < x2) {
343 // scale [x1, x2] to [y1, y2] using Hermite interp
344 float t = (nits - x1) / h12;
345 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
346 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
347 } else {
348 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
349 float t = (nits - x2) / h23;
350 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
351 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
352 }
353 }
354
355 // convert back to [0.0, 1.0]
356 float targetY = nits / maxOutLumi;
357 return color * (targetY / max(1e-6, colorY));
358 }
359 )__SHADER__";
360 break;
361 case Key::INPUT_TF_HLG:
362 fs << R"__SHADER__(
363 highp vec3 OOTF(const highp vec3 color) {
364 const float maxOutLumi = 500.0;
365 const float gamma = 1.2 + 0.42 * log(maxOutLumi / 1000.0) / log(10.0);
366 // The formula is:
367 // alpha * pow(Y, gamma - 1.0) * color + beta;
368 // where alpha is 1.0, beta is 0.0 as recommended in
369 // Rec. ITU-R BT.2100-1 TABLE 5.
370 return pow(CalculateY(color), gamma - 1.0) * color;
371 }
372 )__SHADER__";
373 break;
374 default:
375 fs << R"__SHADER__(
376 highp vec3 OOTF(const highp vec3 color) {
377 return color;
378 }
379 )__SHADER__";
380 }
381
382 // Generate OETF that converts relative display light to signal values,
383 // both normalized to [0, 1]
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800384 switch (needs.getOutputTF()) {
385 case Key::OUTPUT_TF_LINEAR:
386 default:
387 fs << R"__SHADER__(
388 vec3 OETF(const vec3 linear) {
389 return linear;
390 }
391 )__SHADER__";
392 break;
393 case Key::OUTPUT_TF_SRGB:
394 fs << R"__SHADER__(
395 float OETF_sRGB(const float linear) {
396 return linear <= 0.0031308 ?
397 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
398 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700399
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800400 vec3 OETF_sRGB(const vec3 linear) {
401 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
402 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700403
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800404 vec3 OETF(const vec3 linear) {
405 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
406 }
407 )__SHADER__";
408 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800409 case Key::OUTPUT_TF_ST2084:
410 fs << R"__SHADER__(
411 vec3 OETF(const vec3 linear) {
412 const float m1 = (2610.0 / 4096.0) / 4.0;
413 const float m2 = (2523.0 / 4096.0) * 128.0;
414 const float c1 = (3424.0 / 4096.0);
415 const float c2 = (2413.0 / 4096.0) * 32.0;
416 const float c3 = (2392.0 / 4096.0) * 32.0;
417
418 vec3 tmp = pow(linear, vec3(m1));
419 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
420 return pow(tmp, vec3(m2));
421 }
422 )__SHADER__";
423 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700424 case Key::OUTPUT_TF_HLG:
425 fs << R"__SHADER__(
426 highp float OETF_channel(const highp float channel) {
427 const highp float a = 0.17883277;
428 const highp float b = 0.28466892;
429 const highp float c = 0.55991073;
430 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
431 a * log(12.0 * channel - b) + c;
Chia-I Wu131d3762018-01-11 14:35:27 -0800432 }
433
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700434 vec3 OETF(const highp vec3 color) {
435 return vec3(OETF_channel(color.r), OETF_channel(color.g),
436 OETF_channel(color.b));
437 }
438 )__SHADER__";
439 break;
Romain Guy88d37dd2017-05-26 17:57:05 -0700440 }
441 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800442
Mathias Agopian3f844832013-08-07 21:24:32 -0700443 fs << "void main(void) {" << indent;
444 if (needs.isTexturing()) {
445 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800446 if (needs.isY410BT2020()) {
447 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
448 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700449 } else {
chaviw13fdc492017-06-27 12:40:18 -0700450 fs << "gl_FragColor.rgb = color.rgb;";
451 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700452 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700453 if (needs.isOpaque()) {
454 fs << "gl_FragColor.a = 1.0;";
455 }
chaviw13fdc492017-06-27 12:40:18 -0700456 if (needs.hasAlpha()) {
457 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700458 if (needs.isPremultiplied()) {
459 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700460 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700461 } else {
chaviw13fdc492017-06-27 12:40:18 -0700462 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700463 }
464 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700465
466 if (needs.hasColorMatrix()) {
467 if (!needs.isOpaque() && needs.isPremultiplied()) {
468 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700469 // avoid divide by 0 by adding 0.5/256 to the alpha channel
470 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700471 }
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700472 fs << "vec4 transformed = colorMatrix * vec4(OOTF(EOTF(gl_FragColor.rgb)), 1);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800473 // the transformation from a wider colorspace to a narrower one can
474 // result in >1.0 or <0.0 pixel values
475 fs << "transformed.rgb = clamp(transformed.rgb, 0.0, 1.0);";
Romain Guy88d37dd2017-05-26 17:57:05 -0700476 // We assume the last row is always {0,0,0,1} and we skip the division by w
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800477 fs << "gl_FragColor.rgb = OETF(transformed.rgb);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700478 if (!needs.isOpaque() && needs.isPremultiplied()) {
479 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700480 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700481 }
482 }
483
Mathias Agopian3f844832013-08-07 21:24:32 -0700484 fs << dedent << "}";
485 return fs.getString();
486}
487
488Program* ProgramCache::generateProgram(const Key& needs) {
489 // vertex shader
490 String8 vs = generateVertexShader(needs);
491
492 // fragment shader
493 String8 fs = generateFragmentShader(needs);
494
495 Program* program = new Program(needs, vs.string(), fs.string());
496 return program;
497}
498
499void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700500 // generate the key for the shader based on the description
501 Key needs(computeKey(description));
502
Chia-I Wub027f802017-11-29 14:00:52 -0800503 // look-up the program in the cache
Mathias Agopian3f844832013-08-07 21:24:32 -0700504 Program* program = mCache.valueFor(needs);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800505 if (program == nullptr) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700506 // we didn't find our program, so generate one...
507 nsecs_t time = -systemTime();
508 program = generateProgram(needs);
509 mCache.add(needs, program);
510 time += systemTime();
511
Chia-I Wub027f802017-11-29 14:00:52 -0800512 // ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)",
Mathias Agopian3f844832013-08-07 21:24:32 -0700513 // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size());
514 }
515
516 // here we have a suitable program for this description
517 if (program->isValid()) {
518 program->use();
519 program->setUniforms(description);
520 }
521}
522
Mathias Agopian3f844832013-08-07 21:24:32 -0700523} /* namespace android */