Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 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 | #include <GLES2/gl2.h> |
| 18 | #include <GLES2/gl2ext.h> |
| 19 | |
| 20 | #include <utils/String8.h> |
| 21 | |
| 22 | #include "ProgramCache.h" |
| 23 | #include "Program.h" |
| 24 | #include "Description.h" |
| 25 | |
| 26 | namespace android { |
| 27 | // ----------------------------------------------------------------------------------------------- |
| 28 | |
| 29 | |
| 30 | /* |
| 31 | * A simple formatter class to automatically add the endl and |
| 32 | * manage the indentation. |
| 33 | */ |
| 34 | |
| 35 | class Formatter; |
| 36 | static Formatter& indent(Formatter& f); |
| 37 | static Formatter& dedent(Formatter& f); |
| 38 | |
| 39 | class Formatter { |
| 40 | String8 mString; |
| 41 | int mIndent; |
| 42 | typedef Formatter& (*FormaterManipFunc)(Formatter&); |
| 43 | friend Formatter& indent(Formatter& f); |
| 44 | friend Formatter& dedent(Formatter& f); |
| 45 | public: |
Andy McFadden | 892f22d | 2013-08-15 10:05:01 -0700 | [diff] [blame] | 46 | Formatter() : mIndent(0) {} |
| 47 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 48 | String8 getString() const { |
| 49 | return mString; |
| 50 | } |
| 51 | |
| 52 | friend Formatter& operator << (Formatter& out, const char* in) { |
| 53 | for (int i=0 ; i<out.mIndent ; i++) { |
| 54 | out.mString.append(" "); |
| 55 | } |
| 56 | out.mString.append(in); |
| 57 | out.mString.append("\n"); |
| 58 | return out; |
| 59 | } |
| 60 | friend inline Formatter& operator << (Formatter& out, const String8& in) { |
| 61 | return operator << (out, in.string()); |
| 62 | } |
| 63 | friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) { |
| 64 | return (*func)(to); |
| 65 | } |
| 66 | }; |
| 67 | Formatter& indent(Formatter& f) { |
| 68 | f.mIndent++; |
| 69 | return f; |
| 70 | } |
| 71 | Formatter& dedent(Formatter& f) { |
| 72 | f.mIndent--; |
| 73 | return f; |
| 74 | } |
| 75 | |
| 76 | // ----------------------------------------------------------------------------------------------- |
| 77 | |
| 78 | ANDROID_SINGLETON_STATIC_INSTANCE(ProgramCache) |
| 79 | |
| 80 | |
| 81 | ProgramCache::ProgramCache() { |
| 82 | } |
| 83 | |
| 84 | ProgramCache::~ProgramCache() { |
| 85 | } |
| 86 | |
| 87 | ProgramCache::Key ProgramCache::computeKey(const Description& description) { |
| 88 | Key needs; |
| 89 | needs.set(Key::TEXTURE_MASK, |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 90 | !description.mTextureEnabled ? Key::TEXTURE_OFF : |
| 91 | description.mTexture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES ? Key::TEXTURE_EXT : |
| 92 | description.mTexture.getTextureTarget() == GL_TEXTURE_2D ? Key::TEXTURE_2D : |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 93 | Key::TEXTURE_OFF) |
| 94 | .set(Key::PLANE_ALPHA_MASK, |
| 95 | (description.mPlaneAlpha < 1) ? Key::PLANE_ALPHA_LT_ONE : Key::PLANE_ALPHA_EQ_ONE) |
| 96 | .set(Key::BLEND_MASK, |
| 97 | description.mPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL) |
| 98 | .set(Key::OPACITY_MASK, |
| 99 | description.mOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT); |
| 100 | return needs; |
| 101 | } |
| 102 | |
| 103 | String8 ProgramCache::generateVertexShader(const Key& needs) { |
| 104 | Formatter vs; |
| 105 | if (needs.isTexturing()) { |
| 106 | vs << "attribute vec4 texCoords;" |
| 107 | << "varying vec2 outTexCoords;"; |
| 108 | } |
| 109 | vs << "attribute vec4 position;" |
| 110 | << "uniform mat4 projection;" |
| 111 | << "uniform mat4 texture;" |
| 112 | << "void main(void) {" << indent |
| 113 | << "gl_Position = projection * position;"; |
| 114 | if (needs.isTexturing()) { |
| 115 | vs << "outTexCoords = (texture * texCoords).st;"; |
| 116 | } |
| 117 | vs << dedent << "}"; |
| 118 | return vs.getString(); |
| 119 | } |
| 120 | |
| 121 | String8 ProgramCache::generateFragmentShader(const Key& needs) { |
| 122 | Formatter fs; |
| 123 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 124 | fs << "#extension GL_OES_EGL_image_external : require"; |
| 125 | } |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 126 | |
| 127 | // default precision is required-ish in fragment shaders |
| 128 | fs << "precision mediump float;"; |
| 129 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 130 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 131 | fs << "uniform samplerExternalOES sampler;" |
| 132 | << "varying vec2 outTexCoords;"; |
| 133 | } else if (needs.getTextureTarget() == Key::TEXTURE_2D) { |
| 134 | fs << "uniform sampler2D sampler;" |
| 135 | << "varying vec2 outTexCoords;"; |
| 136 | } else if (needs.getTextureTarget() == Key::TEXTURE_OFF) { |
| 137 | fs << "uniform vec4 color;"; |
| 138 | } |
| 139 | if (needs.hasPlaneAlpha()) { |
| 140 | fs << "uniform float alphaPlane;"; |
| 141 | } |
| 142 | fs << "void main(void) {" << indent; |
| 143 | if (needs.isTexturing()) { |
| 144 | fs << "gl_FragColor = texture2D(sampler, outTexCoords);"; |
| 145 | } else { |
| 146 | fs << "gl_FragColor = color;"; |
| 147 | } |
Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 148 | if (needs.isOpaque()) { |
| 149 | fs << "gl_FragColor.a = 1.0;"; |
| 150 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 151 | if (needs.hasPlaneAlpha()) { |
| 152 | // modulate the alpha value with planeAlpha |
| 153 | if (needs.isPremultiplied()) { |
| 154 | // ... and the color too if we're premultiplied |
Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 155 | fs << "gl_FragColor *= alphaPlane;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 156 | } else { |
Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 157 | fs << "gl_FragColor.a *= alphaPlane;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | fs << dedent << "}"; |
| 161 | return fs.getString(); |
| 162 | } |
| 163 | |
| 164 | Program* ProgramCache::generateProgram(const Key& needs) { |
| 165 | // vertex shader |
| 166 | String8 vs = generateVertexShader(needs); |
| 167 | |
| 168 | // fragment shader |
| 169 | String8 fs = generateFragmentShader(needs); |
| 170 | |
| 171 | Program* program = new Program(needs, vs.string(), fs.string()); |
| 172 | return program; |
| 173 | } |
| 174 | |
| 175 | void ProgramCache::useProgram(const Description& description) { |
| 176 | |
| 177 | // generate the key for the shader based on the description |
| 178 | Key needs(computeKey(description)); |
| 179 | |
| 180 | // look-up the program in the cache |
| 181 | Program* program = mCache.valueFor(needs); |
| 182 | if (program == NULL) { |
| 183 | // we didn't find our program, so generate one... |
| 184 | nsecs_t time = -systemTime(); |
| 185 | program = generateProgram(needs); |
| 186 | mCache.add(needs, program); |
| 187 | time += systemTime(); |
| 188 | |
| 189 | //ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)", |
| 190 | // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size()); |
| 191 | } |
| 192 | |
| 193 | // here we have a suitable program for this description |
| 194 | if (program->isValid()) { |
| 195 | program->use(); |
| 196 | program->setUniforms(description); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | } /* namespace android */ |