blob: 33c91b3a68fe80268b7c1eeb49791e55d3dd8061 [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
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050054#define PROGRAM_KEY_COLOR_BLEND 0x40
55#define PROGRAM_KEY_BITMAP_NPOT 0x80
Romain Guyf3a910b42011-12-12 20:35:21 -080056#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
Chris Craik6d29c8d2013-05-08 18:35:44 -070070#define PROGRAM_GRADIENT_TYPE_SHIFT 33 // 2 bits for gradient type
Romain Guyf3a910b42011-12-12 20:35:21 -080071#define PROGRAM_MODULATE_SHIFT 35
72
Chris Craik6d29c8d2013-05-08 18:35:44 -070073#define PROGRAM_HAS_AA_SHIFT 36
Romain Guyf3a910b42011-12-12 20:35:21 -080074
Chris Craik6d29c8d2013-05-08 18:35:44 -070075#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 37
76#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 38
Romain Guyf3a910b42011-12-12 20:35:21 -080077
Chris Craik6d29c8d2013-05-08 18:35:44 -070078#define PROGRAM_HAS_GAMMA_CORRECTION 39
Romain Guyf3a910b42011-12-12 20:35:21 -080079
Chris Craik6d29c8d2013-05-08 18:35:44 -070080#define PROGRAM_IS_SIMPLE_GRADIENT 40
Romain Guy41210632012-07-16 17:04:24 -070081
Chris Craik6d29c8d2013-05-08 18:35:44 -070082#define PROGRAM_HAS_COLORS 41
Romain Guy42e1e0d2012-07-30 14:47:51 -070083
Chris Craik6d29c8d2013-05-08 18:35:44 -070084#define PROGRAM_HAS_DEBUG_HIGHLIGHT 42
85#define PROGRAM_EMULATE_STENCIL 43
Romain Guy3ff0bfd2013-02-25 14:15:37 -080086
Romain Guyf3a910b42011-12-12 20:35:21 -080087///////////////////////////////////////////////////////////////////////////////
88// Types
89///////////////////////////////////////////////////////////////////////////////
90
91typedef uint64_t programid;
92
93///////////////////////////////////////////////////////////////////////////////
94// Program description
95///////////////////////////////////////////////////////////////////////////////
96
97/**
98 * Describe the features required for a given program. The features
99 * determine the generation of both the vertex and fragment shaders.
100 * A ProgramDescription must be used in conjunction with a ProgramCache.
101 */
102struct ProgramDescription {
103 enum ColorModifier {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700104 kColorNone = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800105 kColorMatrix,
Romain Guyf3a910b42011-12-12 20:35:21 -0800106 kColorBlend
107 };
108
109 enum Gradient {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700110 kGradientLinear = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800111 kGradientCircular,
112 kGradientSweep
113 };
114
115 ProgramDescription() {
116 reset();
117 }
118
119 // Texturing
120 bool hasTexture;
121 bool hasAlpha8Texture;
122 bool hasExternalTexture;
123 bool hasTextureTransform;
124
Romain Guyff316ec2013-02-13 18:39:43 -0800125 // Color attribute
126 bool hasColors;
127
Romain Guyf3a910b42011-12-12 20:35:21 -0800128 // Modulate, this should only be set when setColor() return true
129 bool modulate;
130
131 // Shaders
132 bool hasBitmap;
133 bool isBitmapNpot;
134
Chris Craik65cd6122012-12-10 17:56:27 -0800135 bool isAA; // drawing with a per-vertex alpha
Romain Guyf3a910b42011-12-12 20:35:21 -0800136
137 bool hasGradient;
138 Gradient gradientType;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700139 bool isSimpleGradient;
Romain Guyf3a910b42011-12-12 20:35:21 -0800140
141 SkXfermode::Mode shadersMode;
142
143 bool isBitmapFirst;
144 GLenum bitmapWrapS;
145 GLenum bitmapWrapT;
146
147 // Color operations
148 ColorModifier colorOp;
149 SkXfermode::Mode colorMode;
150
151 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
152 // Ignored for all values < SkXfermode::kPlus_Mode
153 SkXfermode::Mode framebufferMode;
154 bool swapSrcDst;
155
Romain Guy41210632012-07-16 17:04:24 -0700156 bool hasGammaCorrection;
157 float gamma;
158
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800159 bool hasDebugHighlight;
Romain Guy78dd96d2013-05-03 14:24:16 -0700160 bool emulateStencil;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800161
Romain Guyf3a910b42011-12-12 20:35:21 -0800162 /**
163 * Resets this description. All fields are reset back to the default
164 * values they hold after building a new instance.
165 */
166 void reset() {
167 hasTexture = false;
168 hasAlpha8Texture = false;
169 hasExternalTexture = false;
170 hasTextureTransform = false;
171
Romain Guyff316ec2013-02-13 18:39:43 -0800172 hasColors = false;
173
Romain Guyf3a910b42011-12-12 20:35:21 -0800174 isAA = false;
175
176 modulate = false;
177
178 hasBitmap = false;
179 isBitmapNpot = false;
180
181 hasGradient = false;
182 gradientType = kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700183 isSimpleGradient = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800184
185 shadersMode = SkXfermode::kClear_Mode;
186
187 isBitmapFirst = false;
188 bitmapWrapS = GL_CLAMP_TO_EDGE;
189 bitmapWrapT = GL_CLAMP_TO_EDGE;
190
191 colorOp = kColorNone;
192 colorMode = SkXfermode::kClear_Mode;
193
194 framebufferMode = SkXfermode::kClear_Mode;
195 swapSrcDst = false;
196
Romain Guy41210632012-07-16 17:04:24 -0700197 hasGammaCorrection = false;
198 gamma = 2.2f;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800199
200 hasDebugHighlight = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800201 }
202
203 /**
204 * Indicates, for a given color, whether color modulation is required in
205 * the fragment shader. When this method returns true, the program should
206 * be provided with a modulation color.
207 */
Chris Craike63f7c622013-10-17 10:30:55 -0700208 bool setColorModulate(const float a) {
Romain Guya938f562012-09-13 20:31:08 -0700209 modulate = a < COLOR_COMPONENT_THRESHOLD;
Romain Guyf3a910b42011-12-12 20:35:21 -0800210 return modulate;
211 }
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 */
Chris Craike63f7c622013-10-17 10:30:55 -0700218 bool setAlpha8ColorModulate(const float r, const float g, const float b, const float a) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800219 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
220 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
221 return modulate;
222 }
223
224 /**
225 * Computes the unique key identifying this program.
226 */
227 programid key() const {
228 programid key = 0;
229 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
230 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
231 if (hasBitmap) {
232 key |= PROGRAM_KEY_BITMAP;
233 if (isBitmapNpot) {
234 key |= PROGRAM_KEY_BITMAP_NPOT;
235 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
236 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
237 }
238 }
239 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
240 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
241 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
242 if (hasBitmap && hasGradient) {
243 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
244 }
245 switch (colorOp) {
246 case kColorMatrix:
247 key |= PROGRAM_KEY_COLOR_MATRIX;
248 break;
Romain Guyf3a910b42011-12-12 20:35:21 -0800249 case kColorBlend:
250 key |= PROGRAM_KEY_COLOR_BLEND;
251 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
252 break;
253 case kColorNone:
254 break;
255 }
256 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
257 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
258 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
Romain Guyf3a910b42011-12-12 20:35:21 -0800259 if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT;
260 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
261 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700262 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700263 if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT;
Romain Guyff316ec2013-02-13 18:39:43 -0800264 if (hasColors) key |= programid(0x1) << PROGRAM_HAS_COLORS;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800265 if (hasDebugHighlight) key |= programid(0x1) << PROGRAM_HAS_DEBUG_HIGHLIGHT;
Romain Guy78dd96d2013-05-03 14:24:16 -0700266 if (emulateStencil) key |= programid(0x1) << PROGRAM_EMULATE_STENCIL;
Romain Guyf3a910b42011-12-12 20:35:21 -0800267 return key;
268 }
269
270 /**
271 * Logs the specified message followed by the key identifying this program.
272 */
273 void log(const char* message) const {
274#if DEBUG_PROGRAMS
275 programid k = key();
276 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
277 uint32_t(k & 0xffffffff));
278#endif
279 }
280
281private:
Romain Guy41210632012-07-16 17:04:24 -0700282 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800283 switch (wrap) {
284 case GL_CLAMP_TO_EDGE:
285 return 0;
286 case GL_REPEAT:
287 return 1;
288 case GL_MIRRORED_REPEAT:
289 return 2;
290 }
291 return 0;
292 }
293
294}; // struct ProgramDescription
295
Romain Guy5cbbce52010-06-27 22:59:20 -0700296/**
297 * A program holds a vertex and a fragment shader. It offers several utility
298 * methods to query attributes and uniforms.
299 */
Romain Guy889f8d12010-07-29 14:37:42 -0700300class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700301public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800302 enum ShaderBindings {
303 kBindingPosition,
304 kBindingTexCoords
305 };
306
Romain Guy5cbbce52010-06-27 22:59:20 -0700307 /**
308 * Creates a new program with the specified vertex and fragment
309 * shaders sources.
310 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800311 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700312 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700313
314 /**
315 * Binds this program to the GL context.
316 */
Romain Guy6926c722010-07-12 20:20:03 -0700317 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700318
Romain Guy260e1022010-07-12 14:41:06 -0700319 /**
320 * Marks this program as unused. This will not unbind
321 * the program from the GL context.
322 */
Romain Guy6926c722010-07-12 20:20:03 -0700323 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700324
325 /**
Romain Guyac670c02010-07-27 17:39:27 -0700326 * Returns the OpenGL name of the specified attribute.
327 */
328 int getAttrib(const char* name);
329
330 /**
331 * Returns the OpenGL name of the specified uniform.
332 */
333 int getUniform(const char* name);
334
335 /**
Romain Guy260e1022010-07-12 14:41:06 -0700336 * Indicates whether this program is currently in use with
337 * the GL context.
338 */
339 inline bool isInUse() const {
340 return mUse;
341 }
342
Romain Guy889f8d12010-07-29 14:37:42 -0700343 /**
Romain Guy67f27952010-12-07 20:09:23 -0800344 * Indicates whether this program was correctly compiled and linked.
345 */
346 inline bool isInitialized() const {
347 return mInitialized;
348 }
349
350 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700351 * Binds the program with the specified projection, modelView and
352 * transform matrices.
353 */
354 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700355 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700356
357 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700358 * Sets the color associated with this shader.
359 */
360 void setColor(const float r, const float g, const float b, const float a);
361
362 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700363 * Name of the position attribute.
364 */
365 int position;
366
367 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800368 * Name of the texCoords attribute if it exists, -1 otherwise.
369 */
370 int texCoords;
371
372 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700373 * Name of the transform uniform.
374 */
375 int transform;
376
Romain Guy39284b72012-09-26 16:39:40 -0700377 /**
378 * Name of the projection uniform.
379 */
380 int projection;
381
Romain Guy5cbbce52010-06-27 22:59:20 -0700382protected:
383 /**
384 * Adds an attribute with the specified name.
385 *
386 * @return The OpenGL name of the attribute.
387 */
388 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700389
390 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800391 * Binds the specified attribute name to the specified slot.
392 */
393 int bindAttrib(const char* name, ShaderBindings bindingSlot);
394
395 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700396 * Adds a uniform with the specified name.
397 *
398 * @return The OpenGL name of the uniform.
399 */
400 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700401
402private:
403 /**
404 * Compiles the specified shader of the specified type.
405 *
406 * @return The name of the compiled shader.
407 */
408 GLuint buildShader(const char* source, GLenum type);
409
Romain Guy3e263fa2011-12-12 16:47:48 -0800410 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800411 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800412 GLuint mVertexShader;
413 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700414
415 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800416 KeyedVector<const char*, int> mAttributes;
417 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700418
419 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800420 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800421
Romain Guy3b748a42013-04-17 18:54:38 -0700422 // Uniforms caching
Romain Guy05bbde72011-12-09 12:55:37 -0800423 bool mHasColorUniform;
424 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800425
426 bool mHasSampler;
Romain Guy3b748a42013-04-17 18:54:38 -0700427
428 mat4 mProjection;
Chris Craikd04a6b12014-01-29 13:00:33 -0800429 bool mOffset;
Romain Guy5cbbce52010-06-27 22:59:20 -0700430}; // class Program
431
Romain Guy5cbbce52010-06-27 22:59:20 -0700432}; // namespace uirenderer
433}; // namespace android
434
Romain Guy5b3b3522010-10-27 18:57:51 -0700435#endif // ANDROID_HWUI_PROGRAM_H