blob: 7ea5376744431ea97c4b6413648fc5f3d5ce4826 [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
Chris Craik096b8d92013-03-01 11:08:11 -080027#include "Debug.h"
Romain Guy0b9db912010-07-09 18:53:25 -070028#include "Matrix.h"
Romain Guyf3a910b42011-12-12 20:35:21 -080029#include "Properties.h"
Romain Guy0b9db912010-07-09 18:53:25 -070030
Romain Guy5cbbce52010-06-27 22:59:20 -070031namespace android {
32namespace uirenderer {
33
Romain Guyf3a910b42011-12-12 20:35:21 -080034///////////////////////////////////////////////////////////////////////////////
35// Defines
36///////////////////////////////////////////////////////////////////////////////
37
38// Debug
39#if DEBUG_PROGRAMS
Steve Block5baa3a62011-12-20 16:23:08 +000040 #define PROGRAM_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf3a910b42011-12-12 20:35:21 -080041#else
42 #define PROGRAM_LOGD(...)
43#endif
44
Romain Guyf8773082012-07-12 18:01:00 -070045#define COLOR_COMPONENT_THRESHOLD 1.0f
46#define COLOR_COMPONENT_INV_THRESHOLD 0.0f
Romain Guyf3a910b42011-12-12 20:35:21 -080047
Chris Craikdeeda3d2014-05-05 19:09:33 -070048#define PROGRAM_KEY_TEXTURE 0x01
49#define PROGRAM_KEY_A8_TEXTURE 0x02
50#define PROGRAM_KEY_BITMAP 0x04
51#define PROGRAM_KEY_GRADIENT 0x08
52#define PROGRAM_KEY_BITMAP_FIRST 0x10
53#define PROGRAM_KEY_COLOR_MATRIX 0x20
54#define PROGRAM_KEY_COLOR_BLEND 0x40
55#define PROGRAM_KEY_BITMAP_NPOT 0x80
Romain Guyf3a910b42011-12-12 20:35:21 -080056
Chris Craikdeeda3d2014-05-05 19:09:33 -070057#define PROGRAM_KEY_SWAP_SRC_DST 0x2000
58
59#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600
Romain Guyf3a910b42011-12-12 20:35:21 -080060#define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800
61
62// Encode the xfermodes on 6 bits
63#define PROGRAM_MAX_XFERMODE 0x1f
64#define PROGRAM_XFERMODE_SHADER_SHIFT 26
65#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20
66#define PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT 14
67
68#define PROGRAM_BITMAP_WRAPS_SHIFT 9
69#define PROGRAM_BITMAP_WRAPT_SHIFT 11
70
Chris Craik6d29c8d2013-05-08 18:35:44 -070071#define PROGRAM_GRADIENT_TYPE_SHIFT 33 // 2 bits for gradient type
Romain Guyf3a910b42011-12-12 20:35:21 -080072#define PROGRAM_MODULATE_SHIFT 35
73
Chris Craik91a8c7c2014-08-12 14:31:35 -070074#define PROGRAM_HAS_VERTEX_ALPHA_SHIFT 36
75#define PROGRAM_USE_SHADOW_ALPHA_INTERP_SHIFT 37
Romain Guyf3a910b42011-12-12 20:35:21 -080076
Chris Craikbf759452014-08-11 16:00:44 -070077#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38
78#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 39
Romain Guyf3a910b42011-12-12 20:35:21 -080079
Chris Craikbf759452014-08-11 16:00:44 -070080#define PROGRAM_HAS_GAMMA_CORRECTION 40
Romain Guyf3a910b42011-12-12 20:35:21 -080081
Chris Craikbf759452014-08-11 16:00:44 -070082#define PROGRAM_IS_SIMPLE_GRADIENT 41
Romain Guy41210632012-07-16 17:04:24 -070083
Chris Craikbf759452014-08-11 16:00:44 -070084#define PROGRAM_HAS_COLORS 42
Romain Guy42e1e0d2012-07-30 14:47:51 -070085
Chris Craikbf759452014-08-11 16:00:44 -070086#define PROGRAM_HAS_DEBUG_HIGHLIGHT 43
87#define PROGRAM_EMULATE_STENCIL 44
88#define PROGRAM_HAS_ROUND_RECT_CLIP 45
Romain Guy3ff0bfd2013-02-25 14:15:37 -080089
Romain Guyf3a910b42011-12-12 20:35:21 -080090///////////////////////////////////////////////////////////////////////////////
91// Types
92///////////////////////////////////////////////////////////////////////////////
93
94typedef uint64_t programid;
95
96///////////////////////////////////////////////////////////////////////////////
97// Program description
98///////////////////////////////////////////////////////////////////////////////
99
100/**
101 * Describe the features required for a given program. The features
102 * determine the generation of both the vertex and fragment shaders.
103 * A ProgramDescription must be used in conjunction with a ProgramCache.
104 */
105struct ProgramDescription {
106 enum ColorModifier {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700107 kColorNone = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800108 kColorMatrix,
Romain Guyf3a910b42011-12-12 20:35:21 -0800109 kColorBlend
110 };
111
112 enum Gradient {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700113 kGradientLinear = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800114 kGradientCircular,
115 kGradientSweep
116 };
117
118 ProgramDescription() {
119 reset();
120 }
121
122 // Texturing
123 bool hasTexture;
124 bool hasAlpha8Texture;
125 bool hasExternalTexture;
126 bool hasTextureTransform;
127
Romain Guyff316ec2013-02-13 18:39:43 -0800128 // Color attribute
129 bool hasColors;
130
Romain Guyf3a910b42011-12-12 20:35:21 -0800131 // Modulate, this should only be set when setColor() return true
132 bool modulate;
133
134 // Shaders
135 bool hasBitmap;
136 bool isBitmapNpot;
137
Chris Craik91a8c7c2014-08-12 14:31:35 -0700138 bool hasVertexAlpha;
139 bool useShadowAlphaInterp;
Romain Guyf3a910b42011-12-12 20:35:21 -0800140
141 bool hasGradient;
142 Gradient gradientType;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700143 bool isSimpleGradient;
Romain Guyf3a910b42011-12-12 20:35:21 -0800144
145 SkXfermode::Mode shadersMode;
146
147 bool isBitmapFirst;
148 GLenum bitmapWrapS;
149 GLenum bitmapWrapT;
150
151 // Color operations
152 ColorModifier colorOp;
153 SkXfermode::Mode colorMode;
154
155 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
156 // Ignored for all values < SkXfermode::kPlus_Mode
157 SkXfermode::Mode framebufferMode;
158 bool swapSrcDst;
159
Romain Guy41210632012-07-16 17:04:24 -0700160 bool hasGammaCorrection;
161 float gamma;
162
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800163 bool hasDebugHighlight;
Romain Guy78dd96d2013-05-03 14:24:16 -0700164 bool emulateStencil;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700165 bool hasRoundRectClip;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800166
Romain Guyf3a910b42011-12-12 20:35:21 -0800167 /**
168 * Resets this description. All fields are reset back to the default
169 * values they hold after building a new instance.
170 */
171 void reset() {
172 hasTexture = false;
173 hasAlpha8Texture = false;
174 hasExternalTexture = false;
175 hasTextureTransform = false;
176
Romain Guyff316ec2013-02-13 18:39:43 -0800177 hasColors = false;
178
Chris Craik91a8c7c2014-08-12 14:31:35 -0700179 hasVertexAlpha = false;
180 useShadowAlphaInterp = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800181
182 modulate = false;
183
184 hasBitmap = false;
185 isBitmapNpot = false;
186
187 hasGradient = false;
188 gradientType = kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700189 isSimpleGradient = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800190
191 shadersMode = SkXfermode::kClear_Mode;
192
193 isBitmapFirst = false;
194 bitmapWrapS = GL_CLAMP_TO_EDGE;
195 bitmapWrapT = GL_CLAMP_TO_EDGE;
196
197 colorOp = kColorNone;
198 colorMode = SkXfermode::kClear_Mode;
199
200 framebufferMode = SkXfermode::kClear_Mode;
201 swapSrcDst = false;
202
Romain Guy41210632012-07-16 17:04:24 -0700203 hasGammaCorrection = false;
204 gamma = 2.2f;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800205
206 hasDebugHighlight = false;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700207 emulateStencil = false;
208 hasRoundRectClip = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800209 }
210
211 /**
212 * Indicates, for a given color, whether color modulation is required in
213 * the fragment shader. When this method returns true, the program should
214 * be provided with a modulation color.
215 */
Chris Craike63f7c622013-10-17 10:30:55 -0700216 bool setColorModulate(const float a) {
Romain Guya938f562012-09-13 20:31:08 -0700217 modulate = a < COLOR_COMPONENT_THRESHOLD;
Romain Guyf3a910b42011-12-12 20:35:21 -0800218 return modulate;
219 }
220
221 /**
222 * Indicates, for a given color, whether color modulation is required in
223 * the fragment shader. When this method returns true, the program should
224 * be provided with a modulation color.
225 */
Chris Craike63f7c622013-10-17 10:30:55 -0700226 bool setAlpha8ColorModulate(const float r, const float g, const float b, const float a) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800227 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
228 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
229 return modulate;
230 }
231
232 /**
233 * Computes the unique key identifying this program.
234 */
235 programid key() const {
236 programid key = 0;
237 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
238 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
239 if (hasBitmap) {
240 key |= PROGRAM_KEY_BITMAP;
241 if (isBitmapNpot) {
242 key |= PROGRAM_KEY_BITMAP_NPOT;
243 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
244 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
245 }
246 }
247 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
248 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
249 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
250 if (hasBitmap && hasGradient) {
251 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
252 }
253 switch (colorOp) {
254 case kColorMatrix:
255 key |= PROGRAM_KEY_COLOR_MATRIX;
256 break;
Romain Guyf3a910b42011-12-12 20:35:21 -0800257 case kColorBlend:
258 key |= PROGRAM_KEY_COLOR_BLEND;
259 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
260 break;
261 case kColorNone:
262 break;
263 }
264 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
265 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
266 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
Chris Craik91a8c7c2014-08-12 14:31:35 -0700267 if (hasVertexAlpha) key |= programid(0x1) << PROGRAM_HAS_VERTEX_ALPHA_SHIFT;
268 if (useShadowAlphaInterp) key |= programid(0x1) << PROGRAM_USE_SHADOW_ALPHA_INTERP_SHIFT;
Romain Guyf3a910b42011-12-12 20:35:21 -0800269 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
270 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700271 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700272 if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT;
Romain Guyff316ec2013-02-13 18:39:43 -0800273 if (hasColors) key |= programid(0x1) << PROGRAM_HAS_COLORS;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800274 if (hasDebugHighlight) key |= programid(0x1) << PROGRAM_HAS_DEBUG_HIGHLIGHT;
Romain Guy78dd96d2013-05-03 14:24:16 -0700275 if (emulateStencil) key |= programid(0x1) << PROGRAM_EMULATE_STENCIL;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700276 if (hasRoundRectClip) key |= programid(0x1) << PROGRAM_HAS_ROUND_RECT_CLIP;
Romain Guyf3a910b42011-12-12 20:35:21 -0800277 return key;
278 }
279
280 /**
281 * Logs the specified message followed by the key identifying this program.
282 */
283 void log(const char* message) const {
284#if DEBUG_PROGRAMS
285 programid k = key();
286 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
287 uint32_t(k & 0xffffffff));
Andreas Gampe42ddc182014-11-21 09:49:08 -0800288#else
289 (void)message;
Romain Guyf3a910b42011-12-12 20:35:21 -0800290#endif
291 }
292
293private:
Romain Guy41210632012-07-16 17:04:24 -0700294 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800295 switch (wrap) {
296 case GL_CLAMP_TO_EDGE:
297 return 0;
298 case GL_REPEAT:
299 return 1;
300 case GL_MIRRORED_REPEAT:
301 return 2;
302 }
303 return 0;
304 }
305
306}; // struct ProgramDescription
307
Romain Guy5cbbce52010-06-27 22:59:20 -0700308/**
309 * A program holds a vertex and a fragment shader. It offers several utility
310 * methods to query attributes and uniforms.
311 */
Romain Guy889f8d12010-07-29 14:37:42 -0700312class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700313public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800314 enum ShaderBindings {
315 kBindingPosition,
316 kBindingTexCoords
317 };
318
Romain Guy5cbbce52010-06-27 22:59:20 -0700319 /**
320 * Creates a new program with the specified vertex and fragment
321 * shaders sources.
322 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800323 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700324 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700325
326 /**
327 * Binds this program to the GL context.
328 */
Romain Guy6926c722010-07-12 20:20:03 -0700329 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700330
Romain Guy260e1022010-07-12 14:41:06 -0700331 /**
332 * Marks this program as unused. This will not unbind
333 * the program from the GL context.
334 */
Romain Guy6926c722010-07-12 20:20:03 -0700335 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700336
337 /**
Romain Guyac670c02010-07-27 17:39:27 -0700338 * Returns the OpenGL name of the specified attribute.
339 */
340 int getAttrib(const char* name);
341
342 /**
343 * Returns the OpenGL name of the specified uniform.
344 */
345 int getUniform(const char* name);
346
347 /**
Romain Guy260e1022010-07-12 14:41:06 -0700348 * Indicates whether this program is currently in use with
349 * the GL context.
350 */
351 inline bool isInUse() const {
352 return mUse;
353 }
354
Romain Guy889f8d12010-07-29 14:37:42 -0700355 /**
Romain Guy67f27952010-12-07 20:09:23 -0800356 * Indicates whether this program was correctly compiled and linked.
357 */
358 inline bool isInitialized() const {
359 return mInitialized;
360 }
361
362 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700363 * Binds the program with the specified projection, modelView and
364 * transform matrices.
365 */
366 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700367 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700368
369 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700370 * Sets the color associated with this shader.
371 */
372 void setColor(const float r, const float g, const float b, const float a);
373
374 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700375 * Name of the position attribute.
376 */
377 int position;
378
379 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800380 * Name of the texCoords attribute if it exists, -1 otherwise.
381 */
382 int texCoords;
383
384 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700385 * Name of the transform uniform.
386 */
387 int transform;
388
Romain Guy39284b72012-09-26 16:39:40 -0700389 /**
390 * Name of the projection uniform.
391 */
392 int projection;
393
Romain Guy5cbbce52010-06-27 22:59:20 -0700394protected:
395 /**
396 * Adds an attribute with the specified name.
397 *
398 * @return The OpenGL name of the attribute.
399 */
400 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700401
402 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800403 * Binds the specified attribute name to the specified slot.
404 */
405 int bindAttrib(const char* name, ShaderBindings bindingSlot);
406
407 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700408 * Adds a uniform with the specified name.
409 *
410 * @return The OpenGL name of the uniform.
411 */
412 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700413
414private:
415 /**
416 * Compiles the specified shader of the specified type.
417 *
418 * @return The name of the compiled shader.
419 */
420 GLuint buildShader(const char* source, GLenum type);
421
Romain Guy3e263fa2011-12-12 16:47:48 -0800422 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800423 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800424 GLuint mVertexShader;
425 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700426
427 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800428 KeyedVector<const char*, int> mAttributes;
429 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700430
431 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800432 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800433
Romain Guy3b748a42013-04-17 18:54:38 -0700434 // Uniforms caching
Romain Guy05bbde72011-12-09 12:55:37 -0800435 bool mHasColorUniform;
436 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800437
438 bool mHasSampler;
Romain Guy3b748a42013-04-17 18:54:38 -0700439
440 mat4 mProjection;
Chris Craikd04a6b12014-01-29 13:00:33 -0800441 bool mOffset;
Romain Guy5cbbce52010-06-27 22:59:20 -0700442}; // class Program
443
Romain Guy5cbbce52010-06-27 22:59:20 -0700444}; // namespace uirenderer
445}; // namespace android
446
Romain Guy5b3b3522010-10-27 18:57:51 -0700447#endif // ANDROID_HWUI_PROGRAM_H