blob: 5a6ec33c3b77d7d9141b4518b3df568dd0ad5f80 [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
17#ifndef ANDROID_UI_PROGRAM_CACHE_H
18#define ANDROID_UI_PROGRAM_CACHE_H
19
20#include <utils/KeyedVector.h>
21#include <utils/Log.h>
22
Romain Guy889f8d12010-07-29 14:37:42 -070023#include <GLES2/gl2.h>
24
Romain Guyac670c02010-07-27 17:39:27 -070025#include <SkXfermode.h>
26
27#include "Program.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
36// Debug
Romain Guy889f8d12010-07-29 14:37:42 -070037#define DEBUG_PROGRAM_CACHE 1
Romain Guyac670c02010-07-27 17:39:27 -070038
39// Debug
40#if DEBUG_PROGRAM_CACHE
41 #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__)
42#else
43 #define PROGRAM_LOGD(...)
44#endif
45
46#define PROGRAM_KEY_TEXTURE 0x1
47#define PROGRAM_KEY_A8_TEXTURE 0x2
48#define PROGRAM_KEY_BITMAP 0x4
49#define PROGRAM_KEY_GRADIENT 0x8
50#define PROGRAM_KEY_BITMAP_FIRST 0x10
51#define PROGRAM_KEY_COLOR_MATRIX 0x20
52#define PROGRAM_KEY_COLOR_LIGHTING 0x40
53#define PROGRAM_KEY_COLOR_BLEND 0x80
Romain Guy889f8d12010-07-29 14:37:42 -070054#define PROGRAM_KEY_BITMAP_NPOT 0x100
55
56#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600
57#define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800
Romain Guyac670c02010-07-27 17:39:27 -070058
59// Support only the 12 Porter-Duff modes for now
60#define PROGRAM_MAX_XFERMODE 0xC
61#define PROGRAM_XFERMODE_SHADER_SHIFT 24
62#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20
63
Romain Guy889f8d12010-07-29 14:37:42 -070064#define PROGRAM_BITMAP_WRAPS_SHIFT 9
65#define PROGRAM_BITMAP_WRAPT_SHIFT 11
66
Romain Guyac670c02010-07-27 17:39:27 -070067///////////////////////////////////////////////////////////////////////////////
68// Types
69///////////////////////////////////////////////////////////////////////////////
70
71typedef uint32_t programid;
72
73///////////////////////////////////////////////////////////////////////////////
74// Cache
75///////////////////////////////////////////////////////////////////////////////
76
77/**
78 * Describe the features required for a given program. The features
79 * determine the generation of both the vertex and fragment shaders.
80 * A ProgramDescription must be used in conjunction with a ProgramCache.
81 */
82struct ProgramDescription {
83 enum ColorModifier {
84 kColorNone,
85 kColorMatrix,
86 kColorLighting,
87 kColorBlend
88 };
89
90 ProgramDescription():
91 hasTexture(false), hasAlpha8Texture(false),
Romain Guy889f8d12010-07-29 14:37:42 -070092 hasBitmap(false), isBitmapNpot(false), hasGradient(false),
93 shadersMode(SkXfermode::kClear_Mode), isBitmapFirst(false),
94 bitmapWrapS(GL_CLAMP_TO_EDGE), bitmapWrapT(GL_CLAMP_TO_EDGE),
Romain Guyac670c02010-07-27 17:39:27 -070095 colorOp(kColorNone), colorMode(SkXfermode::kClear_Mode) {
96 }
97
98 // Texturing
99 bool hasTexture;
100 bool hasAlpha8Texture;
101
102 // Shaders
103 bool hasBitmap;
Romain Guy889f8d12010-07-29 14:37:42 -0700104 bool isBitmapNpot;
Romain Guyac670c02010-07-27 17:39:27 -0700105 bool hasGradient;
106 SkXfermode::Mode shadersMode;
107 bool isBitmapFirst;
Romain Guy889f8d12010-07-29 14:37:42 -0700108 GLenum bitmapWrapS;
109 GLenum bitmapWrapT;
Romain Guyac670c02010-07-27 17:39:27 -0700110
111 // Color operations
112 int colorOp;
113 SkXfermode::Mode colorMode;
114
Romain Guy889f8d12010-07-29 14:37:42 -0700115 inline uint32_t getEnumForWrap(GLenum wrap) const {
116 switch (wrap) {
117 case GL_CLAMP_TO_EDGE:
118 return 0;
119 case GL_REPEAT:
120 return 1;
121 case GL_MIRRORED_REPEAT:
122 return 2;
123 }
124 return 0;
125 }
126
Romain Guyac670c02010-07-27 17:39:27 -0700127 programid key() const {
128 programid key = 0;
129 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
130 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
Romain Guy889f8d12010-07-29 14:37:42 -0700131 if (hasBitmap) {
132 key |= PROGRAM_KEY_BITMAP;
133 if (isBitmapNpot) {
134 key |= PROGRAM_KEY_BITMAP_NPOT;
135 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
136 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
137 }
138 }
Romain Guyac670c02010-07-27 17:39:27 -0700139 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
140 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
141 if (hasBitmap && hasGradient) {
142 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
143 }
144 switch (colorOp) {
145 case kColorMatrix:
146 key |= PROGRAM_KEY_COLOR_MATRIX;
147 break;
148 case kColorLighting:
149 key |= PROGRAM_KEY_COLOR_LIGHTING;
150 break;
151 case kColorBlend:
152 key |= PROGRAM_KEY_COLOR_BLEND;
153 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
154 break;
155 case kColorNone:
156 break;
157 }
158 return key;
159 }
160}; // struct ProgramDescription
161
162/**
163 * Generates and caches program. Programs are generated based on
164 * ProgramDescriptions.
165 */
166class ProgramCache {
167public:
168 ProgramCache();
169 ~ProgramCache();
170
171 Program* get(const ProgramDescription& description);
172
173 void clear();
174
175private:
176 Program* generateProgram(const ProgramDescription& description, programid key);
177 String8 generateVertexShader(const ProgramDescription& description);
178 String8 generateFragmentShader(const ProgramDescription& description);
179 void generatePorterDuffBlend(String8& shader, const char* name, SkXfermode::Mode mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700180 void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT);
Romain Guyac670c02010-07-27 17:39:27 -0700181
182 KeyedVector<programid, Program*> mCache;
183
184}; // class ProgramCache
185
186}; // namespace uirenderer
187}; // namespace android
188
189#endif // ANDROID_UI_PROGRAM_CACHE_H