blob: dd1aaa2a76bdb2ebc99919a57e9524ff8790d49a [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
48#define PROGRAM_KEY_TEXTURE 0x1
49#define PROGRAM_KEY_A8_TEXTURE 0x2
50#define PROGRAM_KEY_BITMAP 0x4
51#define PROGRAM_KEY_GRADIENT 0x8
52#define PROGRAM_KEY_BITMAP_FIRST 0x10
53#define PROGRAM_KEY_COLOR_MATRIX 0x20
54#define PROGRAM_KEY_COLOR_LIGHTING 0x40
55#define PROGRAM_KEY_COLOR_BLEND 0x80
56#define PROGRAM_KEY_BITMAP_NPOT 0x100
57#define PROGRAM_KEY_SWAP_SRC_DST 0x2000
58
59#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600
60#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
71#define PROGRAM_GRADIENT_TYPE_SHIFT 33
72#define PROGRAM_MODULATE_SHIFT 35
73
74#define PROGRAM_IS_POINT_SHIFT 36
75
76#define PROGRAM_HAS_AA_SHIFT 37
77
78#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38
79#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 39
80
Romain Guy41210632012-07-16 17:04:24 -070081#define PROGRAM_HAS_GAMMA_CORRECTION 40
82
Romain Guy42e1e0d2012-07-30 14:47:51 -070083#define PROGRAM_IS_SIMPLE_GRADIENT 41
84
Romain Guyff316ec2013-02-13 18:39:43 -080085#define PROGRAM_HAS_COLORS 42
86
Romain Guy3ff0bfd2013-02-25 14:15:37 -080087#define PROGRAM_HAS_DEBUG_HIGHLIGHT 43
Romain Guy78dd96d2013-05-03 14:24:16 -070088#define PROGRAM_EMULATE_STENCIL 44
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,
109 kColorLighting,
110 kColorBlend
111 };
112
113 enum Gradient {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700114 kGradientLinear = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800115 kGradientCircular,
116 kGradientSweep
117 };
118
119 ProgramDescription() {
120 reset();
121 }
122
123 // Texturing
124 bool hasTexture;
125 bool hasAlpha8Texture;
126 bool hasExternalTexture;
127 bool hasTextureTransform;
128
Romain Guyff316ec2013-02-13 18:39:43 -0800129 // Color attribute
130 bool hasColors;
131
Romain Guyf3a910b42011-12-12 20:35:21 -0800132 // Modulate, this should only be set when setColor() return true
133 bool modulate;
134
135 // Shaders
136 bool hasBitmap;
137 bool isBitmapNpot;
138
Chris Craik65cd6122012-12-10 17:56:27 -0800139 bool isAA; // drawing with a per-vertex alpha
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
160 bool isPoint;
161 float pointSize;
162
Romain Guy41210632012-07-16 17:04:24 -0700163 bool hasGammaCorrection;
164 float gamma;
165
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800166 bool hasDebugHighlight;
Romain Guy78dd96d2013-05-03 14:24:16 -0700167 bool emulateStencil;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800168
Romain Guyf3a910b42011-12-12 20:35:21 -0800169 /**
170 * Resets this description. All fields are reset back to the default
171 * values they hold after building a new instance.
172 */
173 void reset() {
174 hasTexture = false;
175 hasAlpha8Texture = false;
176 hasExternalTexture = false;
177 hasTextureTransform = false;
178
Romain Guyff316ec2013-02-13 18:39:43 -0800179 hasColors = false;
180
Romain Guyf3a910b42011-12-12 20:35:21 -0800181 isAA = false;
182
183 modulate = false;
184
185 hasBitmap = false;
186 isBitmapNpot = false;
187
188 hasGradient = false;
189 gradientType = kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700190 isSimpleGradient = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800191
192 shadersMode = SkXfermode::kClear_Mode;
193
194 isBitmapFirst = false;
195 bitmapWrapS = GL_CLAMP_TO_EDGE;
196 bitmapWrapT = GL_CLAMP_TO_EDGE;
197
198 colorOp = kColorNone;
199 colorMode = SkXfermode::kClear_Mode;
200
201 framebufferMode = SkXfermode::kClear_Mode;
202 swapSrcDst = false;
203
204 isPoint = false;
205 pointSize = 0.0f;
Romain Guy41210632012-07-16 17:04:24 -0700206
207 hasGammaCorrection = false;
208 gamma = 2.2f;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800209
210 hasDebugHighlight = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800211 }
212
213 /**
214 * Indicates, for a given color, whether color modulation is required in
215 * the fragment shader. When this method returns true, the program should
216 * be provided with a modulation color.
217 */
218 bool setColor(const float r, const float g, const float b, const float a) {
Romain Guya938f562012-09-13 20:31:08 -0700219 modulate = a < COLOR_COMPONENT_THRESHOLD;
Romain Guyf3a910b42011-12-12 20:35:21 -0800220 return modulate;
221 }
222
223 /**
224 * Indicates, for a given color, whether color modulation is required in
225 * the fragment shader. When this method returns true, the program should
226 * be provided with a modulation color.
227 */
228 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
229 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
230 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
231 return modulate;
232 }
233
234 /**
235 * Computes the unique key identifying this program.
236 */
237 programid key() const {
238 programid key = 0;
239 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
240 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
241 if (hasBitmap) {
242 key |= PROGRAM_KEY_BITMAP;
243 if (isBitmapNpot) {
244 key |= PROGRAM_KEY_BITMAP_NPOT;
245 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
246 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
247 }
248 }
249 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
250 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
251 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
252 if (hasBitmap && hasGradient) {
253 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
254 }
255 switch (colorOp) {
256 case kColorMatrix:
257 key |= PROGRAM_KEY_COLOR_MATRIX;
258 break;
259 case kColorLighting:
260 key |= PROGRAM_KEY_COLOR_LIGHTING;
261 break;
262 case kColorBlend:
263 key |= PROGRAM_KEY_COLOR_BLEND;
264 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
265 break;
266 case kColorNone:
267 break;
268 }
269 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
270 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
271 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
272 if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT;
273 if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT;
274 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
275 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700276 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700277 if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT;
Romain Guyff316ec2013-02-13 18:39:43 -0800278 if (hasColors) key |= programid(0x1) << PROGRAM_HAS_COLORS;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800279 if (hasDebugHighlight) key |= programid(0x1) << PROGRAM_HAS_DEBUG_HIGHLIGHT;
Romain Guy78dd96d2013-05-03 14:24:16 -0700280 if (emulateStencil) key |= programid(0x1) << PROGRAM_EMULATE_STENCIL;
Romain Guyf3a910b42011-12-12 20:35:21 -0800281 return key;
282 }
283
284 /**
285 * Logs the specified message followed by the key identifying this program.
286 */
287 void log(const char* message) const {
288#if DEBUG_PROGRAMS
289 programid k = key();
290 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
291 uint32_t(k & 0xffffffff));
292#endif
293 }
294
295private:
Romain Guy41210632012-07-16 17:04:24 -0700296 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800297 switch (wrap) {
298 case GL_CLAMP_TO_EDGE:
299 return 0;
300 case GL_REPEAT:
301 return 1;
302 case GL_MIRRORED_REPEAT:
303 return 2;
304 }
305 return 0;
306 }
307
308}; // struct ProgramDescription
309
Romain Guy5cbbce52010-06-27 22:59:20 -0700310/**
311 * A program holds a vertex and a fragment shader. It offers several utility
312 * methods to query attributes and uniforms.
313 */
Romain Guy889f8d12010-07-29 14:37:42 -0700314class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700315public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800316 enum ShaderBindings {
317 kBindingPosition,
318 kBindingTexCoords
319 };
320
Romain Guy5cbbce52010-06-27 22:59:20 -0700321 /**
322 * Creates a new program with the specified vertex and fragment
323 * shaders sources.
324 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800325 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700326 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700327
328 /**
329 * Binds this program to the GL context.
330 */
Romain Guy6926c722010-07-12 20:20:03 -0700331 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700332
Romain Guy260e1022010-07-12 14:41:06 -0700333 /**
334 * Marks this program as unused. This will not unbind
335 * the program from the GL context.
336 */
Romain Guy6926c722010-07-12 20:20:03 -0700337 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700338
339 /**
Romain Guyac670c02010-07-27 17:39:27 -0700340 * Returns the OpenGL name of the specified attribute.
341 */
342 int getAttrib(const char* name);
343
344 /**
345 * Returns the OpenGL name of the specified uniform.
346 */
347 int getUniform(const char* name);
348
349 /**
Romain Guy260e1022010-07-12 14:41:06 -0700350 * Indicates whether this program is currently in use with
351 * the GL context.
352 */
353 inline bool isInUse() const {
354 return mUse;
355 }
356
Romain Guy889f8d12010-07-29 14:37:42 -0700357 /**
Romain Guy67f27952010-12-07 20:09:23 -0800358 * Indicates whether this program was correctly compiled and linked.
359 */
360 inline bool isInitialized() const {
361 return mInitialized;
362 }
363
364 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700365 * Binds the program with the specified projection, modelView and
366 * transform matrices.
367 */
368 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700369 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700370
371 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700372 * Sets the color associated with this shader.
373 */
374 void setColor(const float r, const float g, const float b, const float a);
375
376 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700377 * Name of the position attribute.
378 */
379 int position;
380
381 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800382 * Name of the texCoords attribute if it exists, -1 otherwise.
383 */
384 int texCoords;
385
386 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700387 * Name of the transform uniform.
388 */
389 int transform;
390
Romain Guy39284b72012-09-26 16:39:40 -0700391 /**
392 * Name of the projection uniform.
393 */
394 int projection;
395
Romain Guy5cbbce52010-06-27 22:59:20 -0700396protected:
397 /**
398 * Adds an attribute with the specified name.
399 *
400 * @return The OpenGL name of the attribute.
401 */
402 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700403
404 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800405 * Binds the specified attribute name to the specified slot.
406 */
407 int bindAttrib(const char* name, ShaderBindings bindingSlot);
408
409 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700410 * Adds a uniform with the specified name.
411 *
412 * @return The OpenGL name of the uniform.
413 */
414 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700415
416private:
417 /**
418 * Compiles the specified shader of the specified type.
419 *
420 * @return The name of the compiled shader.
421 */
422 GLuint buildShader(const char* source, GLenum type);
423
Romain Guy3e263fa2011-12-12 16:47:48 -0800424 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800425 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800426 GLuint mVertexShader;
427 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700428
429 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800430 KeyedVector<const char*, int> mAttributes;
431 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700432
433 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800434 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800435
Romain Guy3b748a42013-04-17 18:54:38 -0700436 // Uniforms caching
Romain Guy05bbde72011-12-09 12:55:37 -0800437 bool mHasColorUniform;
438 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800439
440 bool mHasSampler;
Romain Guy3b748a42013-04-17 18:54:38 -0700441
442 mat4 mProjection;
Romain Guy5cbbce52010-06-27 22:59:20 -0700443}; // class Program
444
Romain Guy5cbbce52010-06-27 22:59:20 -0700445}; // namespace uirenderer
446}; // namespace android
447
Romain Guy5b3b3522010-10-27 18:57:51 -0700448#endif // ANDROID_HWUI_PROGRAM_H