blob: 737d91ba12f910317849c0e32e59c1c34755803a [file] [log] [blame]
Romain Guyac670c02010-07-27 17:39:27 -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_CACHE_H
18#define ANDROID_HWUI_PROGRAM_CACHE_H
Romain Guyac670c02010-07-27 17:39:27 -070019
20#include <utils/KeyedVector.h>
21#include <utils/Log.h>
Romain Guy06f96e22010-07-30 19:18:16 -070022#include <utils/String8.h>
Romain Guyac670c02010-07-27 17:39:27 -070023
Romain Guy889f8d12010-07-29 14:37:42 -070024#include <GLES2/gl2.h>
25
Romain Guyac670c02010-07-27 17:39:27 -070026#include <SkXfermode.h>
27
Romain Guyc15008e2010-11-10 11:59:15 -080028#include "Debug.h"
Romain Guyac670c02010-07-27 17:39:27 -070029#include "Program.h"
30
31namespace android {
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Defines
36///////////////////////////////////////////////////////////////////////////////
37
38// Debug
Romain Guyc15008e2010-11-10 11:59:15 -080039#if DEBUG_PROGRAMS
Romain Guyac670c02010-07-27 17:39:27 -070040 #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__)
41#else
42 #define PROGRAM_LOGD(...)
43#endif
44
Romain Guy707b2f72010-10-11 16:34:59 -070045// TODO: This should be set in properties
Romain Guydbc26d22010-10-11 17:58:29 -070046#define PANEL_BIT_DEPTH 20
Romain Guy707b2f72010-10-11 16:34:59 -070047#define COLOR_COMPONENT_THRESHOLD (1.0f - (0.5f / PANEL_BIT_DEPTH))
48#define COLOR_COMPONENT_INV_THRESHOLD (0.5f / PANEL_BIT_DEPTH)
49
Romain Guyac670c02010-07-27 17:39:27 -070050#define PROGRAM_KEY_TEXTURE 0x1
51#define PROGRAM_KEY_A8_TEXTURE 0x2
52#define PROGRAM_KEY_BITMAP 0x4
53#define PROGRAM_KEY_GRADIENT 0x8
54#define PROGRAM_KEY_BITMAP_FIRST 0x10
55#define PROGRAM_KEY_COLOR_MATRIX 0x20
56#define PROGRAM_KEY_COLOR_LIGHTING 0x40
57#define PROGRAM_KEY_COLOR_BLEND 0x80
Romain Guy889f8d12010-07-29 14:37:42 -070058#define PROGRAM_KEY_BITMAP_NPOT 0x100
Romain Guyf607bdc2010-09-10 19:20:06 -070059#define PROGRAM_KEY_SWAP_SRC_DST 0x2000
Romain Guy889f8d12010-07-29 14:37:42 -070060
61#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600
62#define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800
Romain Guyac670c02010-07-27 17:39:27 -070063
Romain Guy48daa542010-08-10 19:21:34 -070064// Encode the xfermodes on 6 bits
65#define PROGRAM_MAX_XFERMODE 0x1f
66#define PROGRAM_XFERMODE_SHADER_SHIFT 26
Romain Guyac670c02010-07-27 17:39:27 -070067#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20
Romain Guya5aed0d2010-09-09 14:42:43 -070068#define PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT 14
Romain Guyac670c02010-07-27 17:39:27 -070069
Romain Guy889f8d12010-07-29 14:37:42 -070070#define PROGRAM_BITMAP_WRAPS_SHIFT 9
71#define PROGRAM_BITMAP_WRAPT_SHIFT 11
72
Romain Guyee916f12010-09-20 17:53:08 -070073#define PROGRAM_GRADIENT_TYPE_SHIFT 33
Romain Guyed6fcb02011-03-21 13:11:28 -070074#define PROGRAM_MODULATE_SHIFT 35
75
76#define PROGRAM_IS_POINT_SHIFT 36
Romain Guyee916f12010-09-20 17:53:08 -070077
Romain Guyac670c02010-07-27 17:39:27 -070078///////////////////////////////////////////////////////////////////////////////
79// Types
80///////////////////////////////////////////////////////////////////////////////
81
Romain Guyee916f12010-09-20 17:53:08 -070082typedef uint64_t programid;
Romain Guyac670c02010-07-27 17:39:27 -070083
84///////////////////////////////////////////////////////////////////////////////
85// Cache
86///////////////////////////////////////////////////////////////////////////////
87
88/**
89 * Describe the features required for a given program. The features
90 * determine the generation of both the vertex and fragment shaders.
91 * A ProgramDescription must be used in conjunction with a ProgramCache.
92 */
93struct ProgramDescription {
94 enum ColorModifier {
95 kColorNone,
96 kColorMatrix,
97 kColorLighting,
98 kColorBlend
99 };
100
Romain Guyee916f12010-09-20 17:53:08 -0700101 enum Gradient {
102 kGradientLinear,
103 kGradientCircular,
104 kGradientSweep
105 };
106
Romain Guy70ca14e2010-12-13 18:24:33 -0800107 ProgramDescription() {
108 reset();
Romain Guyac670c02010-07-27 17:39:27 -0700109 }
110
111 // Texturing
112 bool hasTexture;
113 bool hasAlpha8Texture;
114
Romain Guy707b2f72010-10-11 16:34:59 -0700115 // Modulate, this should only be set when setColor() return true
116 bool modulate;
117
Romain Guyac670c02010-07-27 17:39:27 -0700118 // Shaders
119 bool hasBitmap;
Romain Guy889f8d12010-07-29 14:37:42 -0700120 bool isBitmapNpot;
Romain Guyee916f12010-09-20 17:53:08 -0700121
Romain Guyac670c02010-07-27 17:39:27 -0700122 bool hasGradient;
Romain Guyee916f12010-09-20 17:53:08 -0700123 Gradient gradientType;
124
Romain Guyac670c02010-07-27 17:39:27 -0700125 SkXfermode::Mode shadersMode;
Romain Guyee916f12010-09-20 17:53:08 -0700126
Romain Guyac670c02010-07-27 17:39:27 -0700127 bool isBitmapFirst;
Romain Guy889f8d12010-07-29 14:37:42 -0700128 GLenum bitmapWrapS;
129 GLenum bitmapWrapT;
Romain Guyac670c02010-07-27 17:39:27 -0700130
131 // Color operations
Romain Guy4afdf662010-10-13 21:31:28 -0700132 ColorModifier colorOp;
Romain Guyac670c02010-07-27 17:39:27 -0700133 SkXfermode::Mode colorMode;
134
Romain Guya5aed0d2010-09-09 14:42:43 -0700135 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
136 // Ignored for all values < SkXfermode::kPlus_Mode
137 SkXfermode::Mode framebufferMode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700138 bool swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -0700139
Romain Guyed6fcb02011-03-21 13:11:28 -0700140 bool isPoint;
141 float pointSize;
142
Romain Guy707b2f72010-10-11 16:34:59 -0700143 /**
Romain Guy70ca14e2010-12-13 18:24:33 -0800144 * Resets this description. All fields are reset back to the default
145 * values they hold after building a new instance.
146 */
147 void reset() {
148 hasTexture = false;
149 hasAlpha8Texture = false;
150
151 modulate = false;
152
153 hasBitmap = false;
154 isBitmapNpot = false;
155
156 hasGradient = false;
157 gradientType = kGradientLinear;
158
159 shadersMode = SkXfermode::kClear_Mode;
160
161 isBitmapFirst = false;
162 bitmapWrapS = GL_CLAMP_TO_EDGE;
163 bitmapWrapT = GL_CLAMP_TO_EDGE;
164
165 colorOp = kColorNone;
166 colorMode = SkXfermode::kClear_Mode;
167
168 framebufferMode = SkXfermode::kClear_Mode;
169 swapSrcDst = false;
Romain Guyed6fcb02011-03-21 13:11:28 -0700170
171 isPoint = false;
172 pointSize = 0.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800173 }
174
175 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700176 * Indicates, for a given color, whether color modulation is required in
177 * the fragment shader. When this method returns true, the program should
178 * be provided with a modulation color.
179 */
180 bool setColor(const float r, const float g, const float b, const float a) {
181 modulate = a < COLOR_COMPONENT_THRESHOLD || r < COLOR_COMPONENT_THRESHOLD ||
182 g < COLOR_COMPONENT_THRESHOLD || b < COLOR_COMPONENT_THRESHOLD;
183 return modulate;
Romain Guy889f8d12010-07-29 14:37:42 -0700184 }
185
Romain Guy707b2f72010-10-11 16:34:59 -0700186 /**
187 * Indicates, for a given color, whether color modulation is required in
188 * the fragment shader. When this method returns true, the program should
189 * be provided with a modulation color.
190 */
191 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
192 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
193 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
194 return modulate;
195 }
196
197 /**
198 * Computes the unique key identifying this program.
199 */
Romain Guyac670c02010-07-27 17:39:27 -0700200 programid key() const {
201 programid key = 0;
202 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
203 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
Romain Guy889f8d12010-07-29 14:37:42 -0700204 if (hasBitmap) {
205 key |= PROGRAM_KEY_BITMAP;
206 if (isBitmapNpot) {
207 key |= PROGRAM_KEY_BITMAP_NPOT;
208 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
209 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
210 }
211 }
Romain Guyac670c02010-07-27 17:39:27 -0700212 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
Romain Guyee916f12010-09-20 17:53:08 -0700213 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
214 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
Romain Guyac670c02010-07-27 17:39:27 -0700215 if (hasBitmap && hasGradient) {
216 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
217 }
218 switch (colorOp) {
219 case kColorMatrix:
220 key |= PROGRAM_KEY_COLOR_MATRIX;
221 break;
222 case kColorLighting:
223 key |= PROGRAM_KEY_COLOR_LIGHTING;
224 break;
225 case kColorBlend:
226 key |= PROGRAM_KEY_COLOR_BLEND;
227 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
228 break;
229 case kColorNone:
230 break;
231 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700232 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
Romain Guyf607bdc2010-09-10 19:20:06 -0700233 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
Romain Guyed6fcb02011-03-21 13:11:28 -0700234 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
235 if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT;
Romain Guyac670c02010-07-27 17:39:27 -0700236 return key;
237 }
Romain Guyee916f12010-09-20 17:53:08 -0700238
Romain Guy707b2f72010-10-11 16:34:59 -0700239 /**
240 * Logs the specified message followed by the key identifying this program.
241 */
Romain Guyee916f12010-09-20 17:53:08 -0700242 void log(const char* message) const {
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700243#if DEBUG_PROGRAMS
Romain Guyee916f12010-09-20 17:53:08 -0700244 programid k = key();
245 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
246 uint32_t(k & 0xffffffff));
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700247#endif
Romain Guyee916f12010-09-20 17:53:08 -0700248 }
Romain Guy707b2f72010-10-11 16:34:59 -0700249
250private:
251 inline uint32_t getEnumForWrap(GLenum wrap) const {
252 switch (wrap) {
253 case GL_CLAMP_TO_EDGE:
254 return 0;
255 case GL_REPEAT:
256 return 1;
257 case GL_MIRRORED_REPEAT:
258 return 2;
259 }
260 return 0;
261 }
262
Romain Guyac670c02010-07-27 17:39:27 -0700263}; // struct ProgramDescription
264
265/**
266 * Generates and caches program. Programs are generated based on
267 * ProgramDescriptions.
268 */
269class ProgramCache {
270public:
271 ProgramCache();
272 ~ProgramCache();
273
274 Program* get(const ProgramDescription& description);
275
276 void clear();
277
278private:
279 Program* generateProgram(const ProgramDescription& description, programid key);
280 String8 generateVertexShader(const ProgramDescription& description);
281 String8 generateFragmentShader(const ProgramDescription& description);
Romain Guy48daa542010-08-10 19:21:34 -0700282 void generateBlend(String8& shader, const char* name, SkXfermode::Mode mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700283 void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT);
Romain Guyac670c02010-07-27 17:39:27 -0700284
Romain Guydb1938e2010-08-02 18:50:22 -0700285 void printLongString(const String8& shader) const;
286
Romain Guyac670c02010-07-27 17:39:27 -0700287 KeyedVector<programid, Program*> mCache;
Romain Guyac670c02010-07-27 17:39:27 -0700288}; // class ProgramCache
289
290}; // namespace uirenderer
291}; // namespace android
292
Romain Guy5b3b3522010-10-27 18:57:51 -0700293#endif // ANDROID_HWUI_PROGRAM_CACHE_H