blob: 1818f829bc19ffcba494552e700df883bbf11d2f [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 Guy42e1e0d2012-07-30 14:47:51 -070082#define PROGRAM_IS_SIMPLE_GRADIENT 41
83
Romain Guyf3a910b42011-12-12 20:35:21 -080084///////////////////////////////////////////////////////////////////////////////
85// Types
86///////////////////////////////////////////////////////////////////////////////
87
88typedef uint64_t programid;
89
90///////////////////////////////////////////////////////////////////////////////
91// Program description
92///////////////////////////////////////////////////////////////////////////////
93
94/**
95 * Describe the features required for a given program. The features
96 * determine the generation of both the vertex and fragment shaders.
97 * A ProgramDescription must be used in conjunction with a ProgramCache.
98 */
99struct ProgramDescription {
100 enum ColorModifier {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700101 kColorNone = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800102 kColorMatrix,
103 kColorLighting,
104 kColorBlend
105 };
106
107 enum Gradient {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700108 kGradientLinear = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800109 kGradientCircular,
110 kGradientSweep
111 };
112
113 ProgramDescription() {
114 reset();
115 }
116
117 // Texturing
118 bool hasTexture;
119 bool hasAlpha8Texture;
120 bool hasExternalTexture;
121 bool hasTextureTransform;
122
123 // Modulate, this should only be set when setColor() return true
124 bool modulate;
125
126 // Shaders
127 bool hasBitmap;
128 bool isBitmapNpot;
129
130 bool isAA;
131
132 bool hasGradient;
133 Gradient gradientType;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700134 bool isSimpleGradient;
Romain Guyf3a910b42011-12-12 20:35:21 -0800135
136 SkXfermode::Mode shadersMode;
137
138 bool isBitmapFirst;
139 GLenum bitmapWrapS;
140 GLenum bitmapWrapT;
141
142 // Color operations
143 ColorModifier colorOp;
144 SkXfermode::Mode colorMode;
145
146 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
147 // Ignored for all values < SkXfermode::kPlus_Mode
148 SkXfermode::Mode framebufferMode;
149 bool swapSrcDst;
150
151 bool isPoint;
152 float pointSize;
153
Romain Guy41210632012-07-16 17:04:24 -0700154 bool hasGammaCorrection;
155 float gamma;
156
Romain Guyf3a910b42011-12-12 20:35:21 -0800157 /**
158 * Resets this description. All fields are reset back to the default
159 * values they hold after building a new instance.
160 */
161 void reset() {
162 hasTexture = false;
163 hasAlpha8Texture = false;
164 hasExternalTexture = false;
165 hasTextureTransform = false;
166
167 isAA = false;
168
169 modulate = false;
170
171 hasBitmap = false;
172 isBitmapNpot = false;
173
174 hasGradient = false;
175 gradientType = kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700176 isSimpleGradient = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800177
178 shadersMode = SkXfermode::kClear_Mode;
179
180 isBitmapFirst = false;
181 bitmapWrapS = GL_CLAMP_TO_EDGE;
182 bitmapWrapT = GL_CLAMP_TO_EDGE;
183
184 colorOp = kColorNone;
185 colorMode = SkXfermode::kClear_Mode;
186
187 framebufferMode = SkXfermode::kClear_Mode;
188 swapSrcDst = false;
189
190 isPoint = false;
191 pointSize = 0.0f;
Romain Guy41210632012-07-16 17:04:24 -0700192
193 hasGammaCorrection = false;
194 gamma = 2.2f;
Romain Guyf3a910b42011-12-12 20:35:21 -0800195 }
196
197 /**
198 * Indicates, for a given color, whether color modulation is required in
199 * the fragment shader. When this method returns true, the program should
200 * be provided with a modulation color.
201 */
202 bool setColor(const float r, const float g, const float b, const float a) {
203 modulate = a < COLOR_COMPONENT_THRESHOLD || r < COLOR_COMPONENT_THRESHOLD ||
204 g < COLOR_COMPONENT_THRESHOLD || b < COLOR_COMPONENT_THRESHOLD;
205 return modulate;
206 }
207
208 /**
209 * Indicates, for a given color, whether color modulation is required in
210 * the fragment shader. When this method returns true, the program should
211 * be provided with a modulation color.
212 */
213 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
214 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
215 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
216 return modulate;
217 }
218
219 /**
220 * Computes the unique key identifying this program.
221 */
222 programid key() const {
223 programid key = 0;
224 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
225 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
226 if (hasBitmap) {
227 key |= PROGRAM_KEY_BITMAP;
228 if (isBitmapNpot) {
229 key |= PROGRAM_KEY_BITMAP_NPOT;
230 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
231 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
232 }
233 }
234 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
235 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
236 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
237 if (hasBitmap && hasGradient) {
238 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
239 }
240 switch (colorOp) {
241 case kColorMatrix:
242 key |= PROGRAM_KEY_COLOR_MATRIX;
243 break;
244 case kColorLighting:
245 key |= PROGRAM_KEY_COLOR_LIGHTING;
246 break;
247 case kColorBlend:
248 key |= PROGRAM_KEY_COLOR_BLEND;
249 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
250 break;
251 case kColorNone:
252 break;
253 }
254 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
255 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
256 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
257 if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT;
258 if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT;
259 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
260 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700261 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700262 if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT;
Romain Guyf3a910b42011-12-12 20:35:21 -0800263 return key;
264 }
265
266 /**
267 * Logs the specified message followed by the key identifying this program.
268 */
269 void log(const char* message) const {
270#if DEBUG_PROGRAMS
271 programid k = key();
272 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
273 uint32_t(k & 0xffffffff));
274#endif
275 }
276
277private:
Romain Guy41210632012-07-16 17:04:24 -0700278 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800279 switch (wrap) {
280 case GL_CLAMP_TO_EDGE:
281 return 0;
282 case GL_REPEAT:
283 return 1;
284 case GL_MIRRORED_REPEAT:
285 return 2;
286 }
287 return 0;
288 }
289
290}; // struct ProgramDescription
291
Romain Guy5cbbce52010-06-27 22:59:20 -0700292/**
293 * A program holds a vertex and a fragment shader. It offers several utility
294 * methods to query attributes and uniforms.
295 */
Romain Guy889f8d12010-07-29 14:37:42 -0700296class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700297public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800298 enum ShaderBindings {
299 kBindingPosition,
300 kBindingTexCoords
301 };
302
Romain Guy5cbbce52010-06-27 22:59:20 -0700303 /**
304 * Creates a new program with the specified vertex and fragment
305 * shaders sources.
306 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800307 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700308 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700309
310 /**
311 * Binds this program to the GL context.
312 */
Romain Guy6926c722010-07-12 20:20:03 -0700313 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700314
Romain Guy260e1022010-07-12 14:41:06 -0700315 /**
316 * Marks this program as unused. This will not unbind
317 * the program from the GL context.
318 */
Romain Guy6926c722010-07-12 20:20:03 -0700319 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700320
321 /**
Romain Guyac670c02010-07-27 17:39:27 -0700322 * Returns the OpenGL name of the specified attribute.
323 */
324 int getAttrib(const char* name);
325
326 /**
327 * Returns the OpenGL name of the specified uniform.
328 */
329 int getUniform(const char* name);
330
331 /**
Romain Guy260e1022010-07-12 14:41:06 -0700332 * Indicates whether this program is currently in use with
333 * the GL context.
334 */
335 inline bool isInUse() const {
336 return mUse;
337 }
338
Romain Guy889f8d12010-07-29 14:37:42 -0700339 /**
Romain Guy67f27952010-12-07 20:09:23 -0800340 * Indicates whether this program was correctly compiled and linked.
341 */
342 inline bool isInitialized() const {
343 return mInitialized;
344 }
345
346 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700347 * Binds the program with the specified projection, modelView and
348 * transform matrices.
349 */
350 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700351 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700352
353 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700354 * Sets the color associated with this shader.
355 */
356 void setColor(const float r, const float g, const float b, const float a);
357
358 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700359 * Name of the position attribute.
360 */
361 int position;
362
363 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800364 * Name of the texCoords attribute if it exists, -1 otherwise.
365 */
366 int texCoords;
367
368 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700369 * Name of the transform uniform.
370 */
371 int transform;
372
Romain Guy5cbbce52010-06-27 22:59:20 -0700373protected:
374 /**
375 * Adds an attribute with the specified name.
376 *
377 * @return The OpenGL name of the attribute.
378 */
379 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700380
381 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800382 * Binds the specified attribute name to the specified slot.
383 */
384 int bindAttrib(const char* name, ShaderBindings bindingSlot);
385
386 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700387 * Adds a uniform with the specified name.
388 *
389 * @return The OpenGL name of the uniform.
390 */
391 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700392
393private:
394 /**
395 * Compiles the specified shader of the specified type.
396 *
397 * @return The name of the compiled shader.
398 */
399 GLuint buildShader(const char* source, GLenum type);
400
Romain Guy3e263fa2011-12-12 16:47:48 -0800401 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800402 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800403 GLuint mVertexShader;
404 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700405
406 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800407 KeyedVector<const char*, int> mAttributes;
408 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700409
410 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800411 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800412
413 bool mHasColorUniform;
414 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800415
416 bool mHasSampler;
Romain Guy5cbbce52010-06-27 22:59:20 -0700417}; // class Program
418
Romain Guy5cbbce52010-06-27 22:59:20 -0700419}; // namespace uirenderer
420}; // namespace android
421
Romain Guy5b3b3522010-10-27 18:57:51 -0700422#endif // ANDROID_HWUI_PROGRAM_H