blob: 4f94afce9164e5418029203b8771b487bbf862c6 [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
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 Craik6d29c8d2013-05-08 18:35:44 -070074#define PROGRAM_HAS_AA_SHIFT 36
Romain Guyf3a910b42011-12-12 20:35:21 -080075
Chris Craik6d29c8d2013-05-08 18:35:44 -070076#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 37
77#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 38
Romain Guyf3a910b42011-12-12 20:35:21 -080078
Chris Craik6d29c8d2013-05-08 18:35:44 -070079#define PROGRAM_HAS_GAMMA_CORRECTION 39
Romain Guyf3a910b42011-12-12 20:35:21 -080080
Chris Craik6d29c8d2013-05-08 18:35:44 -070081#define PROGRAM_IS_SIMPLE_GRADIENT 40
Romain Guy41210632012-07-16 17:04:24 -070082
Chris Craik6d29c8d2013-05-08 18:35:44 -070083#define PROGRAM_HAS_COLORS 41
Romain Guy42e1e0d2012-07-30 14:47:51 -070084
Chris Craik6d29c8d2013-05-08 18:35:44 -070085#define PROGRAM_HAS_DEBUG_HIGHLIGHT 42
86#define PROGRAM_EMULATE_STENCIL 43
Romain Guy3ff0bfd2013-02-25 14:15:37 -080087
Romain Guyf3a910b42011-12-12 20:35:21 -080088///////////////////////////////////////////////////////////////////////////////
89// Types
90///////////////////////////////////////////////////////////////////////////////
91
92typedef uint64_t programid;
93
94///////////////////////////////////////////////////////////////////////////////
95// Program description
96///////////////////////////////////////////////////////////////////////////////
97
98/**
99 * Describe the features required for a given program. The features
100 * determine the generation of both the vertex and fragment shaders.
101 * A ProgramDescription must be used in conjunction with a ProgramCache.
102 */
103struct ProgramDescription {
104 enum ColorModifier {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700105 kColorNone = 0,
Romain Guyf3a910b42011-12-12 20:35:21 -0800106 kColorMatrix,
107 kColorLighting,
108 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 Craik65cd6122012-12-10 17:56:27 -0800137 bool isAA; // drawing with a per-vertex alpha
Romain Guyf3a910b42011-12-12 20:35:21 -0800138
139 bool hasGradient;
140 Gradient gradientType;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700141 bool isSimpleGradient;
Romain Guyf3a910b42011-12-12 20:35:21 -0800142
143 SkXfermode::Mode shadersMode;
144
145 bool isBitmapFirst;
146 GLenum bitmapWrapS;
147 GLenum bitmapWrapT;
148
149 // Color operations
150 ColorModifier colorOp;
151 SkXfermode::Mode colorMode;
152
153 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
154 // Ignored for all values < SkXfermode::kPlus_Mode
155 SkXfermode::Mode framebufferMode;
156 bool swapSrcDst;
157
Romain Guy41210632012-07-16 17:04:24 -0700158 bool hasGammaCorrection;
159 float gamma;
160
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800161 bool hasDebugHighlight;
Romain Guy78dd96d2013-05-03 14:24:16 -0700162 bool emulateStencil;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800163
Romain Guyf3a910b42011-12-12 20:35:21 -0800164 /**
165 * Resets this description. All fields are reset back to the default
166 * values they hold after building a new instance.
167 */
168 void reset() {
169 hasTexture = false;
170 hasAlpha8Texture = false;
171 hasExternalTexture = false;
172 hasTextureTransform = false;
173
Romain Guyff316ec2013-02-13 18:39:43 -0800174 hasColors = false;
175
Romain Guyf3a910b42011-12-12 20:35:21 -0800176 isAA = false;
177
178 modulate = false;
179
180 hasBitmap = false;
181 isBitmapNpot = false;
182
183 hasGradient = false;
184 gradientType = kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700185 isSimpleGradient = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800186
187 shadersMode = SkXfermode::kClear_Mode;
188
189 isBitmapFirst = false;
190 bitmapWrapS = GL_CLAMP_TO_EDGE;
191 bitmapWrapT = GL_CLAMP_TO_EDGE;
192
193 colorOp = kColorNone;
194 colorMode = SkXfermode::kClear_Mode;
195
196 framebufferMode = SkXfermode::kClear_Mode;
197 swapSrcDst = false;
198
Romain Guy41210632012-07-16 17:04:24 -0700199 hasGammaCorrection = false;
200 gamma = 2.2f;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800201
202 hasDebugHighlight = false;
Romain Guyf3a910b42011-12-12 20:35:21 -0800203 }
204
205 /**
206 * Indicates, for a given color, whether color modulation is required in
207 * the fragment shader. When this method returns true, the program should
208 * be provided with a modulation color.
209 */
210 bool setColor(const float r, const float g, const float b, const float a) {
Romain Guya938f562012-09-13 20:31:08 -0700211 modulate = a < COLOR_COMPONENT_THRESHOLD;
Romain Guyf3a910b42011-12-12 20:35:21 -0800212 return modulate;
213 }
214
215 /**
216 * Indicates, for a given color, whether color modulation is required in
217 * the fragment shader. When this method returns true, the program should
218 * be provided with a modulation color.
219 */
220 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
221 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
222 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
223 return modulate;
224 }
225
226 /**
227 * Computes the unique key identifying this program.
228 */
229 programid key() const {
230 programid key = 0;
231 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
232 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
233 if (hasBitmap) {
234 key |= PROGRAM_KEY_BITMAP;
235 if (isBitmapNpot) {
236 key |= PROGRAM_KEY_BITMAP_NPOT;
237 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
238 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
239 }
240 }
241 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
242 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
243 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
244 if (hasBitmap && hasGradient) {
245 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
246 }
247 switch (colorOp) {
248 case kColorMatrix:
249 key |= PROGRAM_KEY_COLOR_MATRIX;
250 break;
251 case kColorLighting:
252 key |= PROGRAM_KEY_COLOR_LIGHTING;
253 break;
254 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;
Romain Guyf3a910b42011-12-12 20:35:21 -0800264 if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT;
265 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
266 if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
Romain Guy41210632012-07-16 17:04:24 -0700267 if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700268 if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT;
Romain Guyff316ec2013-02-13 18:39:43 -0800269 if (hasColors) key |= programid(0x1) << PROGRAM_HAS_COLORS;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800270 if (hasDebugHighlight) key |= programid(0x1) << PROGRAM_HAS_DEBUG_HIGHLIGHT;
Romain Guy78dd96d2013-05-03 14:24:16 -0700271 if (emulateStencil) key |= programid(0x1) << PROGRAM_EMULATE_STENCIL;
Romain Guyf3a910b42011-12-12 20:35:21 -0800272 return key;
273 }
274
275 /**
276 * Logs the specified message followed by the key identifying this program.
277 */
278 void log(const char* message) const {
279#if DEBUG_PROGRAMS
280 programid k = key();
281 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
282 uint32_t(k & 0xffffffff));
283#endif
284 }
285
286private:
Romain Guy41210632012-07-16 17:04:24 -0700287 static inline uint32_t getEnumForWrap(GLenum wrap) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800288 switch (wrap) {
289 case GL_CLAMP_TO_EDGE:
290 return 0;
291 case GL_REPEAT:
292 return 1;
293 case GL_MIRRORED_REPEAT:
294 return 2;
295 }
296 return 0;
297 }
298
299}; // struct ProgramDescription
300
Romain Guy5cbbce52010-06-27 22:59:20 -0700301/**
302 * A program holds a vertex and a fragment shader. It offers several utility
303 * methods to query attributes and uniforms.
304 */
Romain Guy889f8d12010-07-29 14:37:42 -0700305class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -0700306public:
Romain Guy3e263fa2011-12-12 16:47:48 -0800307 enum ShaderBindings {
308 kBindingPosition,
309 kBindingTexCoords
310 };
311
Romain Guy5cbbce52010-06-27 22:59:20 -0700312 /**
313 * Creates a new program with the specified vertex and fragment
314 * shaders sources.
315 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800316 Program(const ProgramDescription& description, const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -0700317 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -0700318
319 /**
320 * Binds this program to the GL context.
321 */
Romain Guy6926c722010-07-12 20:20:03 -0700322 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -0700323
Romain Guy260e1022010-07-12 14:41:06 -0700324 /**
325 * Marks this program as unused. This will not unbind
326 * the program from the GL context.
327 */
Romain Guy6926c722010-07-12 20:20:03 -0700328 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -0700329
330 /**
Romain Guyac670c02010-07-27 17:39:27 -0700331 * Returns the OpenGL name of the specified attribute.
332 */
333 int getAttrib(const char* name);
334
335 /**
336 * Returns the OpenGL name of the specified uniform.
337 */
338 int getUniform(const char* name);
339
340 /**
Romain Guy260e1022010-07-12 14:41:06 -0700341 * Indicates whether this program is currently in use with
342 * the GL context.
343 */
344 inline bool isInUse() const {
345 return mUse;
346 }
347
Romain Guy889f8d12010-07-29 14:37:42 -0700348 /**
Romain Guy67f27952010-12-07 20:09:23 -0800349 * Indicates whether this program was correctly compiled and linked.
350 */
351 inline bool isInitialized() const {
352 return mInitialized;
353 }
354
355 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700356 * Binds the program with the specified projection, modelView and
357 * transform matrices.
358 */
359 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700360 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -0700361
362 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700363 * Sets the color associated with this shader.
364 */
365 void setColor(const float r, const float g, const float b, const float a);
366
367 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700368 * Name of the position attribute.
369 */
370 int position;
371
372 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800373 * Name of the texCoords attribute if it exists, -1 otherwise.
374 */
375 int texCoords;
376
377 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700378 * Name of the transform uniform.
379 */
380 int transform;
381
Romain Guy39284b72012-09-26 16:39:40 -0700382 /**
383 * Name of the projection uniform.
384 */
385 int projection;
386
Romain Guy5cbbce52010-06-27 22:59:20 -0700387protected:
388 /**
389 * Adds an attribute with the specified name.
390 *
391 * @return The OpenGL name of the attribute.
392 */
393 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700394
395 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800396 * Binds the specified attribute name to the specified slot.
397 */
398 int bindAttrib(const char* name, ShaderBindings bindingSlot);
399
400 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700401 * Adds a uniform with the specified name.
402 *
403 * @return The OpenGL name of the uniform.
404 */
405 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700406
407private:
408 /**
409 * Compiles the specified shader of the specified type.
410 *
411 * @return The name of the compiled shader.
412 */
413 GLuint buildShader(const char* source, GLenum type);
414
Romain Guy3e263fa2011-12-12 16:47:48 -0800415 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800416 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800417 GLuint mVertexShader;
418 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700419
420 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800421 KeyedVector<const char*, int> mAttributes;
422 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700423
424 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800425 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800426
Romain Guy3b748a42013-04-17 18:54:38 -0700427 // Uniforms caching
Romain Guy05bbde72011-12-09 12:55:37 -0800428 bool mHasColorUniform;
429 int mColorUniform;
Romain Guy2d4fd362011-12-13 22:00:19 -0800430
431 bool mHasSampler;
Romain Guy3b748a42013-04-17 18:54:38 -0700432
433 mat4 mProjection;
Romain Guy5cbbce52010-06-27 22:59:20 -0700434}; // class Program
435
Romain Guy5cbbce52010-06-27 22:59:20 -0700436}; // namespace uirenderer
437}; // namespace android
438
Romain Guy5b3b3522010-10-27 18:57:51 -0700439#endif // ANDROID_HWUI_PROGRAM_H