blob: 30d23692e203a4a1f2ec37b0db5c75ec80fd383d [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
131 if (needs.hasColorMatrix()) {
132 switch (description.mInputTransferFunction) {
133 case Description::TransferFunction::LINEAR:
134 default:
135 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
136 break;
137 case Description::TransferFunction::SRGB:
138 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
139 break;
140 }
141
142 switch (description.mOutputTransferFunction) {
143 case Description::TransferFunction::LINEAR:
144 default:
145 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
146 break;
147 case Description::TransferFunction::SRGB:
148 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
149 break;
150 }
151 }
152
Mathias Agopian3f844832013-08-07 21:24:32 -0700153 return needs;
154}
155
156String8 ProgramCache::generateVertexShader(const Key& needs) {
157 Formatter vs;
158 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800159 vs << "attribute vec4 texCoords;"
160 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700161 }
162 vs << "attribute vec4 position;"
163 << "uniform mat4 projection;"
164 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800165 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700166 if (needs.isTexturing()) {
167 vs << "outTexCoords = (texture * texCoords).st;";
168 }
169 vs << dedent << "}";
170 return vs.getString();
171}
172
173String8 ProgramCache::generateFragmentShader(const Key& needs) {
174 Formatter fs;
175 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
176 fs << "#extension GL_OES_EGL_image_external : require";
177 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700178
179 // default precision is required-ish in fragment shaders
180 fs << "precision mediump float;";
181
Mathias Agopian3f844832013-08-07 21:24:32 -0700182 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
183 fs << "uniform samplerExternalOES sampler;"
184 << "varying vec2 outTexCoords;";
185 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
186 fs << "uniform sampler2D sampler;"
187 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700188 }
189
190 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700191 fs << "uniform vec4 color;";
192 }
chaviw13fdc492017-06-27 12:40:18 -0700193
Mathias Agopianff2ed702013-09-01 21:36:12 -0700194 if (needs.hasColorMatrix()) {
195 fs << "uniform mat4 colorMatrix;";
Romain Guy88d37dd2017-05-26 17:57:05 -0700196
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800197 switch (needs.getInputTF()) {
198 case Key::INPUT_TF_LINEAR:
199 default:
200 fs << R"__SHADER__(
201 vec3 EOTF(const vec3 linear) {
202 return linear;
203 }
204 )__SHADER__";
205 break;
206 case Key::INPUT_TF_SRGB:
207 fs << R"__SHADER__(
208 float EOTF_sRGB(float srgb) {
209 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
210 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700211
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800212 vec3 EOTF_sRGB(const vec3 srgb) {
213 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
214 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700215
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800216 vec3 EOTF(const vec3 srgb) {
217 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
218 }
219 )__SHADER__";
220 break;
221 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700222
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800223 switch (needs.getOutputTF()) {
224 case Key::OUTPUT_TF_LINEAR:
225 default:
226 fs << R"__SHADER__(
227 vec3 OETF(const vec3 linear) {
228 return linear;
229 }
230 )__SHADER__";
231 break;
232 case Key::OUTPUT_TF_SRGB:
233 fs << R"__SHADER__(
234 float OETF_sRGB(const float linear) {
235 return linear <= 0.0031308 ?
236 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
237 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700238
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800239 vec3 OETF_sRGB(const vec3 linear) {
240 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
241 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700242
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800243 vec3 OETF(const vec3 linear) {
244 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
245 }
246 )__SHADER__";
247 break;
Romain Guy88d37dd2017-05-26 17:57:05 -0700248 }
249 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800250
Mathias Agopian3f844832013-08-07 21:24:32 -0700251 fs << "void main(void) {" << indent;
252 if (needs.isTexturing()) {
253 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
254 } else {
chaviw13fdc492017-06-27 12:40:18 -0700255 fs << "gl_FragColor.rgb = color.rgb;";
256 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700257 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700258 if (needs.isOpaque()) {
259 fs << "gl_FragColor.a = 1.0;";
260 }
chaviw13fdc492017-06-27 12:40:18 -0700261 if (needs.hasAlpha()) {
262 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700263 if (needs.isPremultiplied()) {
264 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700265 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700266 } else {
chaviw13fdc492017-06-27 12:40:18 -0700267 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700268 }
269 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700270
271 if (needs.hasColorMatrix()) {
272 if (!needs.isOpaque() && needs.isPremultiplied()) {
273 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700274 // avoid divide by 0 by adding 0.5/256 to the alpha channel
275 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700276 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800277 fs << "vec4 transformed = colorMatrix * vec4(EOTF(gl_FragColor.rgb), 1);";
Romain Guy88d37dd2017-05-26 17:57:05 -0700278 // 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 -0800279 fs << "gl_FragColor.rgb = OETF(transformed.rgb);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700280 if (!needs.isOpaque() && needs.isPremultiplied()) {
281 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700282 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700283 }
284 }
285
Mathias Agopian3f844832013-08-07 21:24:32 -0700286 fs << dedent << "}";
287 return fs.getString();
288}
289
290Program* ProgramCache::generateProgram(const Key& needs) {
291 // vertex shader
292 String8 vs = generateVertexShader(needs);
293
294 // fragment shader
295 String8 fs = generateFragmentShader(needs);
296
297 Program* program = new Program(needs, vs.string(), fs.string());
298 return program;
299}
300
301void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700302 // generate the key for the shader based on the description
303 Key needs(computeKey(description));
304
Chia-I Wub027f802017-11-29 14:00:52 -0800305 // look-up the program in the cache
Mathias Agopian3f844832013-08-07 21:24:32 -0700306 Program* program = mCache.valueFor(needs);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800307 if (program == nullptr) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 // we didn't find our program, so generate one...
309 nsecs_t time = -systemTime();
310 program = generateProgram(needs);
311 mCache.add(needs, program);
312 time += systemTime();
313
Chia-I Wub027f802017-11-29 14:00:52 -0800314 // ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)",
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size());
316 }
317
318 // here we have a suitable program for this description
319 if (program->isValid()) {
320 program->use();
321 program->setUniforms(description);
322 }
323}
324
Mathias Agopian3f844832013-08-07 21:24:32 -0700325} /* namespace android */