blob: 70909fd622bfd9e82985d9546c1121f6aa982a9c [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
Chet Haase6fca9882011-04-25 14:49:07 -070078#define PROGRAM_HAS_WIDTH_SHIFT 37
79
Romain Guyaa6c24c2011-04-28 18:40:04 -070080#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38
81
Romain Guyac670c02010-07-27 17:39:27 -070082///////////////////////////////////////////////////////////////////////////////
83// Types
84///////////////////////////////////////////////////////////////////////////////
85
Romain Guyee916f12010-09-20 17:53:08 -070086typedef uint64_t programid;
Romain Guyac670c02010-07-27 17:39:27 -070087
88///////////////////////////////////////////////////////////////////////////////
89// Cache
90///////////////////////////////////////////////////////////////////////////////
91
92/**
93 * Describe the features required for a given program. The features
94 * determine the generation of both the vertex and fragment shaders.
95 * A ProgramDescription must be used in conjunction with a ProgramCache.
96 */
97struct ProgramDescription {
98 enum ColorModifier {
99 kColorNone,
100 kColorMatrix,
101 kColorLighting,
102 kColorBlend
103 };
104
Romain Guyee916f12010-09-20 17:53:08 -0700105 enum Gradient {
106 kGradientLinear,
107 kGradientCircular,
108 kGradientSweep
109 };
110
Romain Guy70ca14e2010-12-13 18:24:33 -0800111 ProgramDescription() {
112 reset();
Romain Guyac670c02010-07-27 17:39:27 -0700113 }
114
115 // Texturing
116 bool hasTexture;
117 bool hasAlpha8Texture;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700118 bool hasExternalTexture;
Romain Guyac670c02010-07-27 17:39:27 -0700119
Romain Guy707b2f72010-10-11 16:34:59 -0700120 // Modulate, this should only be set when setColor() return true
121 bool modulate;
122
Romain Guyac670c02010-07-27 17:39:27 -0700123 // Shaders
124 bool hasBitmap;
Romain Guy889f8d12010-07-29 14:37:42 -0700125 bool isBitmapNpot;
Romain Guyee916f12010-09-20 17:53:08 -0700126
Chet Haase5b0200b2011-04-13 17:58:08 -0700127 bool hasWidth;
128
Romain Guyac670c02010-07-27 17:39:27 -0700129 bool hasGradient;
Romain Guyee916f12010-09-20 17:53:08 -0700130 Gradient gradientType;
131
Romain Guyac670c02010-07-27 17:39:27 -0700132 SkXfermode::Mode shadersMode;
Romain Guyee916f12010-09-20 17:53:08 -0700133
Romain Guyac670c02010-07-27 17:39:27 -0700134 bool isBitmapFirst;
Romain Guy889f8d12010-07-29 14:37:42 -0700135 GLenum bitmapWrapS;
136 GLenum bitmapWrapT;
Romain Guyac670c02010-07-27 17:39:27 -0700137
138 // Color operations
Romain Guy4afdf662010-10-13 21:31:28 -0700139 ColorModifier colorOp;
Romain Guyac670c02010-07-27 17:39:27 -0700140 SkXfermode::Mode colorMode;
141
Romain Guya5aed0d2010-09-09 14:42:43 -0700142 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
143 // Ignored for all values < SkXfermode::kPlus_Mode
144 SkXfermode::Mode framebufferMode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700145 bool swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -0700146
Romain Guyed6fcb02011-03-21 13:11:28 -0700147 bool isPoint;
148 float pointSize;
149
Romain Guy707b2f72010-10-11 16:34:59 -0700150 /**
Romain Guy70ca14e2010-12-13 18:24:33 -0800151 * Resets this description. All fields are reset back to the default
152 * values they hold after building a new instance.
153 */
154 void reset() {
155 hasTexture = false;
156 hasAlpha8Texture = false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700157 hasExternalTexture = false;
Romain Guy70ca14e2010-12-13 18:24:33 -0800158
Chet Haase5b0200b2011-04-13 17:58:08 -0700159 hasWidth = false;
160
Romain Guy70ca14e2010-12-13 18:24:33 -0800161 modulate = false;
162
163 hasBitmap = false;
164 isBitmapNpot = false;
165
166 hasGradient = false;
167 gradientType = kGradientLinear;
168
169 shadersMode = SkXfermode::kClear_Mode;
170
171 isBitmapFirst = false;
172 bitmapWrapS = GL_CLAMP_TO_EDGE;
173 bitmapWrapT = GL_CLAMP_TO_EDGE;
174
175 colorOp = kColorNone;
176 colorMode = SkXfermode::kClear_Mode;
177
178 framebufferMode = SkXfermode::kClear_Mode;
179 swapSrcDst = false;
Romain Guyed6fcb02011-03-21 13:11:28 -0700180
181 isPoint = false;
182 pointSize = 0.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800183 }
184
185 /**
Romain Guy707b2f72010-10-11 16:34:59 -0700186 * Indicates, for a given color, whether color modulation is required in
187 * the fragment shader. When this method returns true, the program should
188 * be provided with a modulation color.
189 */
190 bool setColor(const float r, const float g, const float b, const float a) {
191 modulate = a < COLOR_COMPONENT_THRESHOLD || r < COLOR_COMPONENT_THRESHOLD ||
192 g < COLOR_COMPONENT_THRESHOLD || b < COLOR_COMPONENT_THRESHOLD;
193 return modulate;
Romain Guy889f8d12010-07-29 14:37:42 -0700194 }
195
Romain Guy707b2f72010-10-11 16:34:59 -0700196 /**
197 * Indicates, for a given color, whether color modulation is required in
198 * the fragment shader. When this method returns true, the program should
199 * be provided with a modulation color.
200 */
201 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
202 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
203 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
204 return modulate;
205 }
206
207 /**
208 * Computes the unique key identifying this program.
209 */
Romain Guyac670c02010-07-27 17:39:27 -0700210 programid key() const {
211 programid key = 0;
212 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
213 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
Romain Guy889f8d12010-07-29 14:37:42 -0700214 if (hasBitmap) {
215 key |= PROGRAM_KEY_BITMAP;
216 if (isBitmapNpot) {
217 key |= PROGRAM_KEY_BITMAP_NPOT;
218 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
219 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
220 }
221 }
Romain Guyac670c02010-07-27 17:39:27 -0700222 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
Romain Guyee916f12010-09-20 17:53:08 -0700223 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
224 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
Romain Guyac670c02010-07-27 17:39:27 -0700225 if (hasBitmap && hasGradient) {
226 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
227 }
228 switch (colorOp) {
229 case kColorMatrix:
230 key |= PROGRAM_KEY_COLOR_MATRIX;
231 break;
232 case kColorLighting:
233 key |= PROGRAM_KEY_COLOR_LIGHTING;
234 break;
235 case kColorBlend:
236 key |= PROGRAM_KEY_COLOR_BLEND;
237 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
238 break;
239 case kColorNone:
240 break;
241 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700242 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
Romain Guyf607bdc2010-09-10 19:20:06 -0700243 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
Romain Guyed6fcb02011-03-21 13:11:28 -0700244 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
245 if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT;
Chet Haase6fca9882011-04-25 14:49:07 -0700246 if (hasWidth) key |= programid(0x1) << PROGRAM_HAS_WIDTH_SHIFT;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700247 if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
Romain Guyac670c02010-07-27 17:39:27 -0700248 return key;
249 }
Romain Guyee916f12010-09-20 17:53:08 -0700250
Romain Guy707b2f72010-10-11 16:34:59 -0700251 /**
252 * Logs the specified message followed by the key identifying this program.
253 */
Romain Guyee916f12010-09-20 17:53:08 -0700254 void log(const char* message) const {
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700255#if DEBUG_PROGRAMS
Romain Guyee916f12010-09-20 17:53:08 -0700256 programid k = key();
257 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
258 uint32_t(k & 0xffffffff));
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700259#endif
Romain Guyee916f12010-09-20 17:53:08 -0700260 }
Romain Guy707b2f72010-10-11 16:34:59 -0700261
262private:
263 inline uint32_t getEnumForWrap(GLenum wrap) const {
264 switch (wrap) {
265 case GL_CLAMP_TO_EDGE:
266 return 0;
267 case GL_REPEAT:
268 return 1;
269 case GL_MIRRORED_REPEAT:
270 return 2;
271 }
272 return 0;
273 }
274
Romain Guyac670c02010-07-27 17:39:27 -0700275}; // struct ProgramDescription
276
277/**
278 * Generates and caches program. Programs are generated based on
279 * ProgramDescriptions.
280 */
281class ProgramCache {
282public:
283 ProgramCache();
284 ~ProgramCache();
285
286 Program* get(const ProgramDescription& description);
287
288 void clear();
289
290private:
291 Program* generateProgram(const ProgramDescription& description, programid key);
292 String8 generateVertexShader(const ProgramDescription& description);
293 String8 generateFragmentShader(const ProgramDescription& description);
Romain Guy48daa542010-08-10 19:21:34 -0700294 void generateBlend(String8& shader, const char* name, SkXfermode::Mode mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700295 void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT);
Romain Guyac670c02010-07-27 17:39:27 -0700296
Romain Guydb1938e2010-08-02 18:50:22 -0700297 void printLongString(const String8& shader) const;
298
Romain Guyac670c02010-07-27 17:39:27 -0700299 KeyedVector<programid, Program*> mCache;
Romain Guyac670c02010-07-27 17:39:27 -0700300}; // class ProgramCache
301
302}; // namespace uirenderer
303}; // namespace android
304
Romain Guy5b3b3522010-10-27 18:57:51 -0700305#endif // ANDROID_HWUI_PROGRAM_CACHE_H