blob: d05b331909e2d79d4b3fa86e1f34941208b47a07 [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
John Reck23d307c2014-10-27 12:38:48 -070087#define PROGRAM_HAS_ROUND_RECT_CLIP 44
Romain Guy3ff0bfd2013-02-25 14:15:37 -080088
Romain Guyf3a910b42011-12-12 20:35:21 -080089///////////////////////////////////////////////////////////////////////////////
90// Types
91///////////////////////////////////////////////////////////////////////////////
92
93typedef uint64_t programid;
94
95///////////////////////////////////////////////////////////////////////////////
96// Program description
97///////////////////////////////////////////////////////////////////////////////
98
99/**
100 * Describe the features required for a given program. The features
101 * determine the generation of both the vertex and fragment shaders.
102 * A ProgramDescription must be used in conjunction with a ProgramCache.
103 */
104struct ProgramDescription {
105 enum ColorModifier {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700106 kColorNone = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800107 kColorMatrix,
Romain Guyf3a910b42011-12-12 20:35:21 -0800108 kColorBlend
109 };
110
111 enum Gradient {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700112 kGradientLinear = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800113 kGradientCircular,
114 kGradientSweep
115 };
116
117 ProgramDescription() {
118 reset();
119 }
120
121 // Texturing
122 bool hasTexture;
123 bool hasAlpha8Texture;
124 bool hasExternalTexture;
125 bool hasTextureTransform;
126
Romain Guyff316ec2013-02-13 18:39:43 -0800127 // Color attribute
128 bool hasColors;
129
Romain Guyf3a910b42011-12-12 20:35:21 -0800130 // Modulate, this should only be set when setColor() return true
131 bool modulate;
132
133 // Shaders
134 bool hasBitmap;
135 bool isBitmapNpot;
136
Chris Craik91a8c7c2014-08-12 14:31:35 -0700137 bool hasVertexAlpha;
138 bool useShadowAlphaInterp;
Romain Guyf3a910b42011-12-12 20:35:21 -0800139
140 bool hasGradient;
141 Gradient gradientType;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700142 bool isSimpleGradient;
Romain Guyf3a910b42011-12-12 20:35:21 -0800143
144 SkXfermode::Mode shadersMode;
145
146 bool isBitmapFirst;
147 GLenum bitmapWrapS;
148 GLenum bitmapWrapT;
149
150 // Color operations
151 ColorModifier colorOp;
152 SkXfermode::Mode colorMode;
153
154 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
155 // Ignored for all values < SkXfermode::kPlus_Mode
156 SkXfermode::Mode framebufferMode;
157 bool swapSrcDst;
158
Romain Guy41210632012-07-16 17:04:24 -0700159 bool hasGammaCorrection;
160 float gamma;
161
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800162 bool hasDebugHighlight;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700163 bool hasRoundRectClip;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800164
Romain Guyf3a910b42011-12-12 20:35:21 -0800165 /**
166 * Resets this description. All fields are reset back to the default
167 * values they hold after building a new instance.
168 */
169 void reset() {
170 hasTexture = false;
171 hasAlpha8Texture = false;
172 hasExternalTexture = false;
173 hasTextureTransform = false;
174
Romain Guyff316ec2013-02-13 18:39:43 -0800175 hasColors = false;
176
Chris Craik91a8c7c2014-08-12 14:31:35 -0700177 hasVertexAlpha = false;
178 useShadowAlphaInterp = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800179
180 modulate = false;
181
182 hasBitmap = false;
183 isBitmapNpot = false;
184
185 hasGradient = false;
186 gradientType = kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700187 isSimpleGradient = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800188
189 shadersMode = SkXfermode::kClear_Mode;
190
191 isBitmapFirst = false;
192 bitmapWrapS = GL_CLAMP_TO_EDGE;
193 bitmapWrapT = GL_CLAMP_TO_EDGE;
194
195 colorOp = kColorNone;
196 colorMode = SkXfermode::kClear_Mode;
197
198 framebufferMode = SkXfermode::kClear_Mode;
199 swapSrcDst = false;
200
Romain Guy41210632012-07-16 17:04:24 -0700201 hasGammaCorrection = false;
202 gamma = 2.2f;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800203
204 hasDebugHighlight = false;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700205 hasRoundRectClip = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800206 }
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 */
Chris Craike63f7c62013-10-17 10:30:55 -0700213 bool setColorModulate(const float a) {
Romain Guya938f562012-09-13 20:31:08 -0700214 modulate = a < COLOR_COMPONENT_THRESHOLD;
Romain Guyf3a910b42011-12-12 20:35:21 -0800215 return modulate;
216 }
217
218 /**
219 * Indicates, for a given color, whether color modulation is required in
220 * the fragment shader. When this method returns true, the program should
221 * be provided with a modulation color.
222 */
Chris Craike63f7c62013-10-17 10:30:55 -0700223 bool setAlpha8ColorModulate(const float r, const float g, const float b, const float a) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800224 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
225 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
226 return modulate;
227 }
228
229 /**
230 * Computes the unique key identifying this program.
231 */
232 programid key() const {
233 programid key = 0;
234 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
235 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
236 if (hasBitmap) {
237 key |= PROGRAM_KEY_BITMAP;
238 if (isBitmapNpot) {
239 key |= PROGRAM_KEY_BITMAP_NPOT;
240 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
241 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
242 }
243 }
244 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
245 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
246 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
247 if (hasBitmap && hasGradient) {
248 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
249 }
250 switch (colorOp) {
251 case kColorMatrix:
252 key |= PROGRAM_KEY_COLOR_MATRIX;
253 break;
Romain Guyf3a910b42011-12-12 20:35:21 -0800254 case kColorBlend:
255 key |= PROGRAM_KEY_COLOR_BLEND;
256 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
257 break;
258 case kColorNone:
259 break;
260 }
261 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
262 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
263 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
Chris Craik91a8c7c2014-08-12 14:31:35 -0700264 if (hasVertexAlpha) key |= programid(0x1) << PROGRAM_HAS_VERTEX_ALPHA_SHIFT;
265 if (useShadowAlphaInterp) key |= programid(0x1) << PROGRAM_USE_SHADOW_ALPHA_INTERP_SHIFT;
Romain Guyf3a910b42011-12-12 20:35:21 -0800266 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
267 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700268 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700269 if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT;
Romain Guyff316ec2013-02-13 18:39:43 -0800270 if (hasColors) key |= programid(0x1) << PROGRAM_HAS_COLORS;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800271 if (hasDebugHighlight) key |= programid(0x1) << PROGRAM_HAS_DEBUG_HIGHLIGHT;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700272 if (hasRoundRectClip) key |= programid(0x1) << PROGRAM_HAS_ROUND_RECT_CLIP;
Romain Guyf3a910b42011-12-12 20:35:21 -0800273 return key;
274 }
275
276 /**
277 * Logs the specified message followed by the key identifying this program.
278 */
279 void log(const char* message) const {
280#if DEBUG_PROGRAMS
281 programid k = key();
282 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
283 uint32_t(k & 0xffffffff));
284#endif
285 }
286
287private:
Romain Guy41210632012-07-16 17:04:24 -0700288 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800289 switch (wrap) {
290 case GL_CLAMP_TO_EDGE:
291 return 0;
292 case GL_REPEAT:
293 return 1;
294 case GL_MIRRORED_REPEAT:
295 return 2;
296 }
297 return 0;
298 }
299
300}; // struct ProgramDescription
301
Romain Guy5cbbce52010-06-27 22:59:20 -0700302/**
303 * A program holds a vertex and a fragment shader. It offers several utility
304 * methods to query attributes and uniforms.
305 */
Romain Guy889f8d12010-07-29 14:37:42 -0700306class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700307public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800308 enum ShaderBindings {
309 kBindingPosition,
310 kBindingTexCoords
311 };
312
Romain Guy5cbbce52010-06-27 22:59:20 -0700313 /**
314 * Creates a new program with the specified vertex and fragment
315 * shaders sources.
316 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800317 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700318 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700319
320 /**
321 * Binds this program to the GL context.
322 */
Romain Guy6926c722010-07-12 20:20:03 -0700323 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700324
Romain Guy260e1022010-07-12 14:41:06 -0700325 /**
326 * Marks this program as unused. This will not unbind
327 * the program from the GL context.
328 */
Romain Guy6926c722010-07-12 20:20:03 -0700329 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700330
331 /**
Romain Guyac670c02010-07-27 17:39:27 -0700332 * Returns the OpenGL name of the specified attribute.
333 */
334 int getAttrib(const char* name);
335
336 /**
337 * Returns the OpenGL name of the specified uniform.
338 */
339 int getUniform(const char* name);
340
341 /**
Romain Guy260e1022010-07-12 14:41:06 -0700342 * Indicates whether this program is currently in use with
343 * the GL context.
344 */
345 inline bool isInUse() const {
346 return mUse;
347 }
348
Romain Guy889f8d12010-07-29 14:37:42 -0700349 /**
Romain Guy67f27952010-12-07 20:09:23 -0800350 * Indicates whether this program was correctly compiled and linked.
351 */
352 inline bool isInitialized() const {
353 return mInitialized;
354 }
355
356 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700357 * Binds the program with the specified projection, modelView and
358 * transform matrices.
359 */
360 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700361 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700362
363 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700364 * Sets the color associated with this shader.
365 */
366 void setColor(const float r, const float g, const float b, const float a);
367
368 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700369 * Name of the position attribute.
370 */
371 int position;
372
373 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800374 * Name of the texCoords attribute if it exists, -1 otherwise.
375 */
376 int texCoords;
377
378 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700379 * Name of the transform uniform.
380 */
381 int transform;
382
Romain Guy39284b72012-09-26 16:39:40 -0700383 /**
384 * Name of the projection uniform.
385 */
386 int projection;
387
Romain Guy5cbbce52010-06-27 22:59:20 -0700388protected:
389 /**
390 * Adds an attribute with the specified name.
391 *
392 * @return The OpenGL name of the attribute.
393 */
394 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700395
396 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800397 * Binds the specified attribute name to the specified slot.
398 */
399 int bindAttrib(const char* name, ShaderBindings bindingSlot);
400
401 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700402 * Adds a uniform with the specified name.
403 *
404 * @return The OpenGL name of the uniform.
405 */
406 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700407
408private:
409 /**
410 * Compiles the specified shader of the specified type.
411 *
412 * @return The name of the compiled shader.
413 */
414 GLuint buildShader(const char* source, GLenum type);
415
Romain Guy3e263fa2011-12-12 16:47:48 -0800416 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800417 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800418 GLuint mVertexShader;
419 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700420
421 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800422 KeyedVector<const char*, int> mAttributes;
423 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700424
425 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800426 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800427
Romain Guy3b748a42013-04-17 18:54:38 -0700428 // Uniforms caching
Romain Guy05bbde72011-12-09 12:55:37 -0800429 bool mHasColorUniform;
430 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800431
432 bool mHasSampler;
Romain Guy3b748a42013-04-17 18:54:38 -0700433
434 mat4 mProjection;
Chris Craikd04a6b12014-01-29 13:00:33 -0800435 bool mOffset;
Romain Guy5cbbce52010-06-27 22:59:20 -0700436}; // class Program
437
Romain Guy5cbbce52010-06-27 22:59:20 -0700438}; // namespace uirenderer
439}; // namespace android
440
Romain Guy5b3b3522010-10-27 18:57:51 -0700441#endif // ANDROID_HWUI_PROGRAM_H