blob: fc3e248713f3dce07690f4c70d6444a2b63fd6d6 [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 Guy707b2f72010-10-11 16:34:59 -070074#define PROGRAM_MODULATE 35
Romain Guyee916f12010-09-20 17:53:08 -070075
Romain Guyac670c02010-07-27 17:39:27 -070076///////////////////////////////////////////////////////////////////////////////
77// Types
78///////////////////////////////////////////////////////////////////////////////
79
Romain Guyee916f12010-09-20 17:53:08 -070080typedef uint64_t programid;
Romain Guyac670c02010-07-27 17:39:27 -070081
82///////////////////////////////////////////////////////////////////////////////
83// Cache
84///////////////////////////////////////////////////////////////////////////////
85
86/**
87 * Describe the features required for a given program. The features
88 * determine the generation of both the vertex and fragment shaders.
89 * A ProgramDescription must be used in conjunction with a ProgramCache.
90 */
91struct ProgramDescription {
92 enum ColorModifier {
93 kColorNone,
94 kColorMatrix,
95 kColorLighting,
96 kColorBlend
97 };
98
Romain Guyee916f12010-09-20 17:53:08 -070099 enum Gradient {
100 kGradientLinear,
101 kGradientCircular,
102 kGradientSweep
103 };
104
Romain Guyac670c02010-07-27 17:39:27 -0700105 ProgramDescription():
Romain Guy707b2f72010-10-11 16:34:59 -0700106 hasTexture(false), hasAlpha8Texture(false), modulate(false),
Romain Guy889f8d12010-07-29 14:37:42 -0700107 hasBitmap(false), isBitmapNpot(false), hasGradient(false),
Romain Guyee916f12010-09-20 17:53:08 -0700108 gradientType(kGradientLinear),
Romain Guy889f8d12010-07-29 14:37:42 -0700109 shadersMode(SkXfermode::kClear_Mode), isBitmapFirst(false),
110 bitmapWrapS(GL_CLAMP_TO_EDGE), bitmapWrapT(GL_CLAMP_TO_EDGE),
Romain Guya5aed0d2010-09-09 14:42:43 -0700111 colorOp(kColorNone), colorMode(SkXfermode::kClear_Mode),
Romain Guyf607bdc2010-09-10 19:20:06 -0700112 framebufferMode(SkXfermode::kClear_Mode), swapSrcDst(false) {
Romain Guyac670c02010-07-27 17:39:27 -0700113 }
114
115 // Texturing
116 bool hasTexture;
117 bool hasAlpha8Texture;
118
Romain Guy707b2f72010-10-11 16:34:59 -0700119 // Modulate, this should only be set when setColor() return true
120 bool modulate;
121
Romain Guyac670c02010-07-27 17:39:27 -0700122 // Shaders
123 bool hasBitmap;
Romain Guy889f8d12010-07-29 14:37:42 -0700124 bool isBitmapNpot;
Romain Guyee916f12010-09-20 17:53:08 -0700125
Romain Guyac670c02010-07-27 17:39:27 -0700126 bool hasGradient;
Romain Guyee916f12010-09-20 17:53:08 -0700127 Gradient gradientType;
128
Romain Guyac670c02010-07-27 17:39:27 -0700129 SkXfermode::Mode shadersMode;
Romain Guyee916f12010-09-20 17:53:08 -0700130
Romain Guyac670c02010-07-27 17:39:27 -0700131 bool isBitmapFirst;
Romain Guy889f8d12010-07-29 14:37:42 -0700132 GLenum bitmapWrapS;
133 GLenum bitmapWrapT;
Romain Guyac670c02010-07-27 17:39:27 -0700134
135 // Color operations
Romain Guy4afdf662010-10-13 21:31:28 -0700136 ColorModifier colorOp;
Romain Guyac670c02010-07-27 17:39:27 -0700137 SkXfermode::Mode colorMode;
138
Romain Guya5aed0d2010-09-09 14:42:43 -0700139 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
140 // Ignored for all values < SkXfermode::kPlus_Mode
141 SkXfermode::Mode framebufferMode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700142 bool swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -0700143
Romain Guy707b2f72010-10-11 16:34:59 -0700144 /**
145 * Indicates, for a given color, whether color modulation is required in
146 * the fragment shader. When this method returns true, the program should
147 * be provided with a modulation color.
148 */
149 bool setColor(const float r, const float g, const float b, const float a) {
150 modulate = a < COLOR_COMPONENT_THRESHOLD || r < COLOR_COMPONENT_THRESHOLD ||
151 g < COLOR_COMPONENT_THRESHOLD || b < COLOR_COMPONENT_THRESHOLD;
152 return modulate;
Romain Guy889f8d12010-07-29 14:37:42 -0700153 }
154
Romain Guy707b2f72010-10-11 16:34:59 -0700155 /**
156 * Indicates, for a given color, whether color modulation is required in
157 * the fragment shader. When this method returns true, the program should
158 * be provided with a modulation color.
159 */
160 bool setAlpha8Color(const float r, const float g, const float b, const float a) {
161 modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD ||
162 g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD;
163 return modulate;
164 }
165
166 /**
167 * Computes the unique key identifying this program.
168 */
Romain Guyac670c02010-07-27 17:39:27 -0700169 programid key() const {
170 programid key = 0;
171 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
172 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
Romain Guy889f8d12010-07-29 14:37:42 -0700173 if (hasBitmap) {
174 key |= PROGRAM_KEY_BITMAP;
175 if (isBitmapNpot) {
176 key |= PROGRAM_KEY_BITMAP_NPOT;
177 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
178 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
179 }
180 }
Romain Guyac670c02010-07-27 17:39:27 -0700181 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
Romain Guyee916f12010-09-20 17:53:08 -0700182 key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT;
183 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
Romain Guyac670c02010-07-27 17:39:27 -0700184 if (hasBitmap && hasGradient) {
185 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
186 }
187 switch (colorOp) {
188 case kColorMatrix:
189 key |= PROGRAM_KEY_COLOR_MATRIX;
190 break;
191 case kColorLighting:
192 key |= PROGRAM_KEY_COLOR_LIGHTING;
193 break;
194 case kColorBlend:
195 key |= PROGRAM_KEY_COLOR_BLEND;
196 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
197 break;
198 case kColorNone:
199 break;
200 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700201 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
Romain Guyf607bdc2010-09-10 19:20:06 -0700202 if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
Romain Guy707b2f72010-10-11 16:34:59 -0700203 if (modulate) key |= programid(0x1) << PROGRAM_MODULATE;
Romain Guyac670c02010-07-27 17:39:27 -0700204 return key;
205 }
Romain Guyee916f12010-09-20 17:53:08 -0700206
Romain Guy707b2f72010-10-11 16:34:59 -0700207 /**
208 * Logs the specified message followed by the key identifying this program.
209 */
Romain Guyee916f12010-09-20 17:53:08 -0700210 void log(const char* message) const {
211 programid k = key();
212 PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
213 uint32_t(k & 0xffffffff));
214 }
Romain Guy707b2f72010-10-11 16:34:59 -0700215
216private:
217 inline uint32_t getEnumForWrap(GLenum wrap) const {
218 switch (wrap) {
219 case GL_CLAMP_TO_EDGE:
220 return 0;
221 case GL_REPEAT:
222 return 1;
223 case GL_MIRRORED_REPEAT:
224 return 2;
225 }
226 return 0;
227 }
228
Romain Guyac670c02010-07-27 17:39:27 -0700229}; // struct ProgramDescription
230
231/**
232 * Generates and caches program. Programs are generated based on
233 * ProgramDescriptions.
234 */
235class ProgramCache {
236public:
237 ProgramCache();
238 ~ProgramCache();
239
240 Program* get(const ProgramDescription& description);
241
242 void clear();
243
244private:
245 Program* generateProgram(const ProgramDescription& description, programid key);
246 String8 generateVertexShader(const ProgramDescription& description);
247 String8 generateFragmentShader(const ProgramDescription& description);
Romain Guy48daa542010-08-10 19:21:34 -0700248 void generateBlend(String8& shader, const char* name, SkXfermode::Mode mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700249 void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT);
Romain Guyac670c02010-07-27 17:39:27 -0700250
Romain Guydb1938e2010-08-02 18:50:22 -0700251 void printLongString(const String8& shader) const;
252
Romain Guyac670c02010-07-27 17:39:27 -0700253 KeyedVector<programid, Program*> mCache;
Romain Guyac670c02010-07-27 17:39:27 -0700254}; // class ProgramCache
255
256}; // namespace uirenderer
257}; // namespace android
258
Romain Guy5b3b3522010-10-27 18:57:51 -0700259#endif // ANDROID_HWUI_PROGRAM_CACHE_H