blob: 491767fc51bfaceac49ed7dabdff987727b7d8ec [file] [log] [blame]
Romain Guy5cbbce52010-06-27 22:59:20 -07001/*
2 * Copyright (C) 2010 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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_PROGRAM_H
18#define ANDROID_HWUI_PROGRAM_H
Romain Guy5cbbce52010-06-27 22:59:20 -070019
Romain Guyf3a910b42011-12-12 20:35:21 -080020#include <utils/KeyedVector.h>
21
Romain Guy5cbbce52010-06-27 22:59:20 -070022#include <GLES2/gl2.h>
23#include <GLES2/gl2ext.h>
24
Romain Guyf3a910b42011-12-12 20:35:21 -080025#include <SkXfermode.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guy0b9db912010-07-09 18:53:25 -070027#include "Matrix.h"
Romain Guyf3a910b42011-12-12 20:35:21 -080028#include "Properties.h"
Romain Guy0b9db912010-07-09 18:53:25 -070029
Romain Guy5cbbce52010-06-27 22:59:20 -070030namespace android {
31namespace uirenderer {
32
Romain Guyf3a910b42011-12-12 20:35:21 -080033///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// Debug
38#if DEBUG_PROGRAMS
Steve Block5baa3a62011-12-20 16:23:08 +000039 #define PROGRAM_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf3a910b42011-12-12 20:35:21 -080040#else
41 #define PROGRAM_LOGD(...)
42#endif
43
Romain Guyf8773082012-07-12 18:01:00 -070044#define COLOR_COMPONENT_THRESHOLD 1.0f
45#define COLOR_COMPONENT_INV_THRESHOLD 0.0f
Romain Guyf3a910b42011-12-12 20:35:21 -080046
47#define PROGRAM_KEY_TEXTURE 0x1
48#define PROGRAM_KEY_A8_TEXTURE 0x2
49#define PROGRAM_KEY_BITMAP 0x4
50#define PROGRAM_KEY_GRADIENT 0x8
51#define PROGRAM_KEY_BITMAP_FIRST 0x10
52#define PROGRAM_KEY_COLOR_MATRIX 0x20
53#define PROGRAM_KEY_COLOR_LIGHTING 0x40
54#define PROGRAM_KEY_COLOR_BLEND 0x80
55#define PROGRAM_KEY_BITMAP_NPOT 0x100
56#define PROGRAM_KEY_SWAP_SRC_DST 0x2000
57
58#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600
59#define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800
60
61// Encode the xfermodes on 6 bits
62#define PROGRAM_MAX_XFERMODE 0x1f
63#define PROGRAM_XFERMODE_SHADER_SHIFT 26
64#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20
65#define PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT 14
66
67#define PROGRAM_BITMAP_WRAPS_SHIFT 9
68#define PROGRAM_BITMAP_WRAPT_SHIFT 11
69
70#define PROGRAM_GRADIENT_TYPE_SHIFT 33
71#define PROGRAM_MODULATE_SHIFT 35
72
73#define PROGRAM_IS_POINT_SHIFT 36
74
75#define PROGRAM_HAS_AA_SHIFT 37
76
77#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38
78#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 39
79
Romain Guy41210632012-07-16 17:04:24 -070080#define PROGRAM_HAS_GAMMA_CORRECTION 40
81
Romain Guyf3a910b42011-12-12 20:35:21 -080082///////////////////////////////////////////////////////////////////////////////
83// Types
84///////////////////////////////////////////////////////////////////////////////
85
86typedef uint64_t programid;
87
88///////////////////////////////////////////////////////////////////////////////
89// Program description
90///////////////////////////////////////////////////////////////////////////////
91
92/**
93 * Describe the features required for a given program. The features
94 * determine the generation of both the vertex and fragment shaders.
95 * A ProgramDescription must be used in conjunction with a ProgramCache.
96 */
97struct ProgramDescription {
98 enum ColorModifier {
99 kColorNone,
100 kColorMatrix,
101 kColorLighting,
102 kColorBlend
103 };
104
105 enum Gradient {
106 kGradientLinear,
107 kGradientCircular,
108 kGradientSweep
109 };
110
111 ProgramDescription() {
112 reset();
113 }
114
115 // Texturing
116 bool hasTexture;
117 bool hasAlpha8Texture;
118 bool hasExternalTexture;
119 bool hasTextureTransform;
120
121 // Modulate, this should only be set when setColor() return true
122 bool modulate;
123
124 // Shaders
125 bool hasBitmap;
126 bool isBitmapNpot;
127
128 bool isAA;
129
130 bool hasGradient;
131 Gradient gradientType;
132
133 SkXfermode::Mode shadersMode;
134
135 bool isBitmapFirst;
136 GLenum bitmapWrapS;
137 GLenum bitmapWrapT;
138
139 // Color operations
140 ColorModifier colorOp;
141 SkXfermode::Mode colorMode;
142
143 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
144 // Ignored for all values < SkXfermode::kPlus_Mode
145 SkXfermode::Mode framebufferMode;
146 bool swapSrcDst;
147
148 bool isPoint;
149 float pointSize;
150
Romain Guy41210632012-07-16 17:04:24 -0700151 bool hasGammaCorrection;
152 float gamma;
153
Romain Guyf3a910b42011-12-12 20:35:21 -0800154 /**
155 * Resets this description. All fields are reset back to the default
156 * values they hold after building a new instance.
157 */
158 void reset() {
159 hasTexture = false;
160 hasAlpha8Texture = false;
161 hasExternalTexture = false;
162 hasTextureTransform = false;
163
164 isAA = false;
165
166 modulate = false;
167
168 hasBitmap = false;
169 isBitmapNpot = false;
170
171 hasGradient = false;
172 gradientType = kGradientLinear;
173
174 shadersMode = SkXfermode::kClear_Mode;
175
176 isBitmapFirst = false;
177 bitmapWrapS = GL_CLAMP_TO_EDGE;
178 bitmapWrapT = GL_CLAMP_TO_EDGE;
179
180 colorOp = kColorNone;
181 colorMode = SkXfermode::kClear_Mode;
182
183 framebufferMode = SkXfermode::kClear_Mode;
184 swapSrcDst = false;
185
186 isPoint = false;
187 pointSize = 0.0f;
Romain Guy41210632012-07-16 17:04:24 -0700188
189 hasGammaCorrection = false;
190 gamma = 2.2f;
Romain Guyf3a910b42011-12-12 20:35:21 -0800191 }
192
193 /**
194 * Indicates, for a given color, whether color modulation is required in
195 * the fragment shader. When this method returns true, the program should
196 * be provided with a modulation color.
197 */
198 bool setColor(const float r, const float g, const float b, const float a) {
199 modulate = a < COLOR_COMPONENT_THRESHOLD || r < COLOR_COMPONENT_THRESHOLD ||
200 g < COLOR_COMPONENT_THRESHOLD || b < COLOR_COMPONENT_THRESHOLD;
201 return modulate;
202 }
203
204 /**
205 * Indicates, for a given color, whether color modulation is required in
206 * the fragment shader. When this method returns true, the program should
207 * be provided with a modulation color.
208 */
209 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
210 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
211 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
212 return modulate;
213 }
214
215 /**
216 * Computes the unique key identifying this program.
217 */
218 programid key() const {
219 programid key = 0;
220 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
221 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
222 if (hasBitmap) {
223 key |= PROGRAM_KEY_BITMAP;
224 if (isBitmapNpot) {
225 key |= PROGRAM_KEY_BITMAP_NPOT;
226 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
227 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
228 }
229 }
230 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
231 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
232 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
233 if (hasBitmap && hasGradient) {
234 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
235 }
236 switch (colorOp) {
237 case kColorMatrix:
238 key |= PROGRAM_KEY_COLOR_MATRIX;
239 break;
240 case kColorLighting:
241 key |= PROGRAM_KEY_COLOR_LIGHTING;
242 break;
243 case kColorBlend:
244 key |= PROGRAM_KEY_COLOR_BLEND;
245 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
246 break;
247 case kColorNone:
248 break;
249 }
250 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
251 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
252 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
253 if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT;
254 if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT;
255 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
256 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700257 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guyf3a910b42011-12-12 20:35:21 -0800258 return key;
259 }
260
261 /**
262 * Logs the specified message followed by the key identifying this program.
263 */
264 void log(const char* message) const {
265#if DEBUG_PROGRAMS
266 programid k = key();
267 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
268 uint32_t(k & 0xffffffff));
269#endif
270 }
271
272private:
Romain Guy41210632012-07-16 17:04:24 -0700273 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800274 switch (wrap) {
275 case GL_CLAMP_TO_EDGE:
276 return 0;
277 case GL_REPEAT:
278 return 1;
279 case GL_MIRRORED_REPEAT:
280 return 2;
281 }
282 return 0;
283 }
284
285}; // struct ProgramDescription
286
Romain Guy5cbbce52010-06-27 22:59:20 -0700287/**
288 * A program holds a vertex and a fragment shader. It offers several utility
289 * methods to query attributes and uniforms.
290 */
Romain Guy889f8d12010-07-29 14:37:42 -0700291class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700292public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800293 enum ShaderBindings {
294 kBindingPosition,
295 kBindingTexCoords
296 };
297
Romain Guy5cbbce52010-06-27 22:59:20 -0700298 /**
299 * Creates a new program with the specified vertex and fragment
300 * shaders sources.
301 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800302 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700303 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700304
305 /**
306 * Binds this program to the GL context.
307 */
Romain Guy6926c722010-07-12 20:20:03 -0700308 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700309
Romain Guy260e1022010-07-12 14:41:06 -0700310 /**
311 * Marks this program as unused. This will not unbind
312 * the program from the GL context.
313 */
Romain Guy6926c722010-07-12 20:20:03 -0700314 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700315
316 /**
Romain Guyac670c02010-07-27 17:39:27 -0700317 * Returns the OpenGL name of the specified attribute.
318 */
319 int getAttrib(const char* name);
320
321 /**
322 * Returns the OpenGL name of the specified uniform.
323 */
324 int getUniform(const char* name);
325
326 /**
Romain Guy260e1022010-07-12 14:41:06 -0700327 * Indicates whether this program is currently in use with
328 * the GL context.
329 */
330 inline bool isInUse() const {
331 return mUse;
332 }
333
Romain Guy889f8d12010-07-29 14:37:42 -0700334 /**
Romain Guy67f27952010-12-07 20:09:23 -0800335 * Indicates whether this program was correctly compiled and linked.
336 */
337 inline bool isInitialized() const {
338 return mInitialized;
339 }
340
341 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700342 * Binds the program with the specified projection, modelView and
343 * transform matrices.
344 */
345 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700346 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700347
348 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700349 * Sets the color associated with this shader.
350 */
351 void setColor(const float r, const float g, const float b, const float a);
352
353 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700354 * Name of the position attribute.
355 */
356 int position;
357
358 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800359 * Name of the texCoords attribute if it exists, -1 otherwise.
360 */
361 int texCoords;
362
363 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700364 * Name of the transform uniform.
365 */
366 int transform;
367
Romain Guy5cbbce52010-06-27 22:59:20 -0700368protected:
369 /**
370 * Adds an attribute with the specified name.
371 *
372 * @return The OpenGL name of the attribute.
373 */
374 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700375
376 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 * Binds the specified attribute name to the specified slot.
378 */
379 int bindAttrib(const char* name, ShaderBindings bindingSlot);
380
381 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700382 * Adds a uniform with the specified name.
383 *
384 * @return The OpenGL name of the uniform.
385 */
386 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700387
388private:
389 /**
390 * Compiles the specified shader of the specified type.
391 *
392 * @return The name of the compiled shader.
393 */
394 GLuint buildShader(const char* source, GLenum type);
395
Romain Guy3e263fa2011-12-12 16:47:48 -0800396 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800397 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800398 GLuint mVertexShader;
399 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700400
401 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800402 KeyedVector<const char*, int> mAttributes;
403 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700404
405 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800406 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800407
408 bool mHasColorUniform;
409 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800410
411 bool mHasSampler;
Romain Guy5cbbce52010-06-27 22:59:20 -0700412}; // class Program
413
Romain Guy5cbbce52010-06-27 22:59:20 -0700414}; // namespace uirenderer
415}; // namespace android
416
Romain Guy5b3b3522010-10-27 18:57:51 -0700417#endif // ANDROID_HWUI_PROGRAM_H